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]


i added re.upto function in lpeg re.lua
function re.upto(what, greedy)
 local fmt = string.format
 local c = fmt('(!(%s).)', what)
 if greedy then c = fmt('((%s) !(%s* !.) / %s+)', what, c, c) end
 return fmt({%s*} (%s) ', c, what)
end
text = 'this and that and whatever'
= re.match(text, re.upto( " 'and' " ) .. "{.*}") -- non-greedy match
this
that and whatever
= re.match(text, re.upto( "[aA][nN][dD]" ) .. "{.*}") -- non-greedy case-insensitive
this
that and whatever
= re.match(text, re.upto( " 'and' ", true) .. "{.*}") -- greedy match
this and that
whatever
plain lpeg upto function if interested:
http://www.gammon.com.au/forum/bbshowpost.php?id=14149&page=2

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