lua-users home
lua-l archive

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


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