I have a predefined code, e.g."12-345-6789"
, and wish to match the first and last portions with Lua patterns, e.g. "12-6789". An exclusion of the second number set and the hyphen should work but I am having trouble figuring that out with patterns or if it is possible.
I know I could capture each individually like so
code = "12-345-6789"
first, middle, last = string.match(code, "(%d+)-(%d+)-(%d+)")
and use that but it would require a lot of code rewriting on my part. I would ideally like to take the current table of pattern matches and add it to be used with string.match
lcPart = { "^(%d+)", "^(%d+%-%d+)", "(%d+)$", ?new pattern here? }
code = "12-345-6789"
newCode = string.match(code, lcPart[4])
You can't do this with one capture, but it's trivial to splice the results of two captures together:
If you're trying to match against a list of patterns, it may be better to refactor it into a list of functions instead: