According to the docs, all Lua repetition operators only work on single characters, so you can match
string.match('123', '1?(%d+)') -- returns 23
but cannot match multi-character strings:
string.match('123', '(12)?(%d+)') -- want this to return 3
The docs say it's possible through "multiple patterns and custom logic", but I don't know what that means. Can someone offer a way to pattern match the above? Basically, 12
should be optionally matched all-or-nothing, and return the remainder of the digit string.
Lua patterns can match sequences of characters, where each character can be optional, or repeat multiple times. If you're used to other languages that have regular expressions to match text, remember that Lua's pattern matching is not the same: it's more limited, and has different syntax.
They all are based on patterns. Unlike several other scripting languages, Lua does not use POSIX regular expressions (regexp) for pattern matching. The main reason for this is size: A typical implementation of POSIX regexp takes more than 4,000 lines of code. This is bigger than all Lua standard libraries together.
find(string, pattern [, init [, plain]]): This function returns to start and end index of a match pattern in a string. match(string, pattern [, index]): This function matches a pattern once the matching starts at the given index.
gsub() function has three arguments, the first is the subject string, in which we are trying to replace a substring to another substring, the second argument is the pattern that we want to replace in the given string, and the third argument is the string from which we want to replace the pattern.
I think "multiple patterns and custom logic" here means usage like this:
string.match('123', '12(%d+)') or string.match('123', '(%d+)')
Since or
is short-circuit, if the first pattern matches, it will be the value of the expresion, otherwise the second pattern will try to match. This is exactly the regex (12)?(%d+)
means.
Also note that there are more powerful LPeg or other regex libraries for Lua.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With