[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lpeg pattern repetition
- From: Nick Gammon <nick@...>
- Date: Mon, 2 Jun 2008 10:35:07 +1000
Another thing I can't get to work is something simple like this:
I want to match on "Nick says hello" where "Nick" can vary. That is,
in regular expression syntax:
.* says hello
I tried to do this in LPeg like so:
print (lpeg.match (lpeg.P (1)^0 * " says hello",
"Nick says hello")) --> nil
I am guessing that the lpeg.P (1) is so greedy it is consuming the "
says hello" part as well. But if that is the case, how can that sort
of thing be easily expressed? Another example would be:
.* says .*
That would be useful for general decoding of conversations. <someone>
says <something>
The only way I can get my first example to work is by specifying the
character set for the name, like this:
print (lpeg.match (lpeg.R ("AZ", "az")^0 * " says hello",
"Nick says hello")) --> 16
That works, but only because I have excluded a space.
If I wanted to match on "Nick the programmer says hello", then I need
a space in the matching set, and then it consumes " says hello" as well.
By way of comparison, this works:
print (string.match ("Nick says hello", ".* says hello")) --> Nick
says hello
Thanks for any helpful suggestions you can make. :)
- Nick Gammon