lua-users home
lua-l archive

Re: how to translate lua pattern "(.*)and(.*)" with lpeg re ?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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

AltStyle によって変換されたページ (->オリジナル) /