1

Here's my question. I'm using Lua and I have a string that looks something like this:

"Start1.2.3.4.5-1.2.3.4.5-1.2.3.4.5-1.2.3.4.5-1.2.3.4.5End"

The five numbers between each hyphen are all paired to the same "object" but each represents a separate set of data. The period between the numbers separates the data.

So after Start, 1 = our first value, 2 = our second value, 3 = our third value, 4 = our fourth value, and 5 = our fifth value. These 5 values are stored to the same object. Then we hit our first hyphen which separates the "objects". So there's 5 objects and 5 values per object.

I used 1.2.3.4.5 as an example but these numbers will be randomized with up to 4 digits. So it could say something like Start12.3.100.1025.50- etc...

Hopefully that makes sense. Here's what I have done so far:

MyString = the long string I posted above

local extracted = string.match(MyString, "Start(.*)")

This returns everything beyond Start in the string. However, I want it to return everything after Start and then cut off once it reaches the next hyphen. Then from that point on I'll repeat the process but instead find everything between the hyphens until I reach End. I also need to filter out the periods. Also, the hyphens/periods can change to something else as long as they aren't numbers.

Any ideas on how to do this?

1 Answer 1

1

Just use a pattern that captures anything that contains numbers and periods.

"([%d%.]+)" Note that you have to escape the period with % as it is a magic character.

local text = "Start1.2.3.4.5-1.2.3.4.5-1.2.3.4.5-1.2.3.4.5-1.2.3.4.5End" for set in text:gmatch("([%d%.]+)") do print(set) local numbers = {} for num in set:gmatch("%d+") do table.insert(numbers, num) end print(table.unpack(numbers)) end 

prints:

1.2.3.4.5 1 2 3 4 5 1.2.3.4.5 1 2 3 4 5 1.2.3.4.5 1 2 3 4 5 1.2.3.4.5 1 2 3 4 5 1.2.3.4.5 1 2 3 4 5 
Sign up to request clarification or add additional context in comments.

2 Comments

Would this work if the number between periods was more than one digit? That was also a facet of his question.
@Corsaka + matches one or more. but even if not I wouldn't consider it a bad thing to leave some room for his own brain power. bearing in mind that this is not a coding service.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.