[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Understanding Strings
- From: Sean Conner <sean@...>
- Date: Mon, 28 May 2012 17:33:01 -0400
It was thus said that the Great Mike McGonagle once stated:
> OK, I'm stumped... I have a string...
>
> "tobj index (0 6 45)"
>
> how do I parse the string to get the left paren? I am using string.find and
> passing a single character which gets used as a pattern. This works fine
> when passed a "{" or a "<", but it chokes on "(" and "[". I just want to
> convert the enclosed data to a sub-table.
>
> None of the tutorials I have read seem to touch on writing such patterns.
> If you could lead the way?
You could try using LPeg. Here's the code that will parse the data:
re = require "re"
G = [[
parser <- [^(]+ '(' items+ -> {} ')'
items <- %s* {[0-9]+} %s*
]]
parser = re.compile(G)
x = parser:match("tobj index (0 6 45)")
for i = 1 , #x do
print(x[i])
end
I personally find this a bit easier to read than regular expressions.
-spc