[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to translate lua pattern "(.*)and(.*)" with lpeg re ?
- From: Nick Gammon <nick@...>
- Date: Sun, 21 Jan 2018 14:08:05 +1100
On 21/01/18 13:18, Sean Conner wrote:
You might want to read the thread starting here:
http://lua-users.org/lists/lua-l/2017-10/msg00126.html
I think relevant to your problem is this post:
http://lua-users.org/lists/lua-l/2017-10/msg00140.html
OK, that was very helpful, thanks Sean.
I worked it out with this grammar:
require "re"
target = "foo and bar and whatever"
c = re.compile [[
parse <- {| {noDelim} lastDelim |} -- look for all up to the last delimiter followed by the last part
delim <- 'and' -- our delimiter
noDelim <- (!lastDelim .)* -- zero or more characters without the last delimiter
lastDelim <- delim {(!delim .)*} !. -- the delimiter without any more delimiters and then end of subject
]]
result = lpeg.match (c, target)
for k, v in ipairs (result) do
print (k, v)
end -- for
Output:
1 foo and bar
2 whatever
- Nick