Nimjox
Nimjox

Reputation: 1381

Lua Parse After Match

Using Lua 5.3 I'm trying to parse a string that looks something like.

a=data0
b=data
c=data
a=data1
b=data
c=data
a=data2
...

I want to parse the 'b=data & c=data' after the occurrence of 'a=data1'. I know I can start by doing string.find(exampleString, 'a=data1') which will give me the start/end position and I know where to start parsing for b after that but I don't know how long 'data' will be after that, so I don't know where to start for parsing 'c'? Is there anyway I can just do a parse next line type of thing? How else should I tackle this?

Upvotes: 1

Views: 71

Answers (1)

Mud
Mud

Reputation: 28991

know where to start parsing for b after that but I don't know how long 'data' will be after that, so I don't know where to start for parsing 'c'? Is there anyway I can just do a parse next line type of thing?

Yes, just can just match to the EOL character:

for letter, data in s:gmatch('(%w)=(.-)\n') do
    print(letter,data)
end

.- = 0 or more characters, as few as possible
\n = EOL character
The parentheses capture parts of the pattern we want gmatch to return.

You could also say ([^\n]*) which means 0 or more character which are not a newline.

Upvotes: 2

Related Questions