Re: how to translate lua pattern "(.*)and(.*)" with lpeg re ?
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: how to translate lua pattern "(.*)and(.*)" with lpeg re ?
- From: albertmcchan <albertmcchan@...>
- Date: 2018年1月23日 14:19:57 -0500
while learning how lpeg '=>' work,
discover a faster re pattern for text with many 'and' or long string length
-- old lua pattern "(.*)and(.*)"
old_pat = re.compile "{g <- &('and' (!'and' .)* !.) / .g} 'and' {.*}"
-- new lua pattern "(.*)and(.*)"
-- speedup by not doing lookahead after 'and'
pat = re.compile(
"(g <- 'and' / .g)+ => split",
{ split = function(s,i) return true, s:sub(1, i-4), s:sub(i) end }
)
= pat:match "this and that and whatever"
this and that
whatever