parsing - Lua string manipulation pattern matching alternative "|" -
is there way can string pattern match "ab|cd" matches either "ab" or "cd" in input string. know use "[ab]" pattern , match either "a" or "b", works 1 letter stuff.
note actual problem lot more complicated, need know if there or thing in lua's string manipulation. want put other patterns on each sides of or thing, , etc. if works "hello|world" , matches "hello, world!" both "hello" , "world" it's great!
just expand on peterm's suggestion, lpeg provides re module exposes similar interface lua's standard string library while still preserving power , flexibility offered lpeg.
i try out re module first since syntax bit less esoteric compared lpeg. here's example usage can match hello world example:
dump = require 'pl.pretty'.dump re = require 're' local subj = "hello, world! padding world1 !hello hello hellonomatch nohello" pat = re.compile [[ toks <- tok (%w+ tok)* tok <- {'hello' / 'world'} !%w / %w+ ]] res = { re.match(subj, pat) } dump(res) which output:
{ "hello", "world", "hello", "hello" } if you're interested in capturing position of matches modify grammar positional capture:
tok <- {}('hello' / 'world') !%w / %w+
Comments
Post a Comment