Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua: pattern matching multi-characters with the ? repetition operator

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.

like image 955
Neil Avatar asked Jul 26 '13 12:07

Neil


People also ask

What is Lua pattern matching?

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.

Does Lua have regex?

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.

How do I use Lua match?

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.

How do I use GSUB Lua?

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.


1 Answers

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.

like image 77
Yu Hao Avatar answered Sep 21 '22 10:09

Yu Hao