Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible match for (seemingly) invalid Lua-pattern

I know that you can't repeat match groups in Lua. For example, if I wanted to match the two successive "45"'s, I can't do:

print(string.find("some 4545 text", "(%d%d)+"))

which will print nil (no match found).

However, since find(...) doesn't report an error (for the invalid patterns "%" and "(%d" errors are produced), it leads me to believe the pattern "(%d%d)+" is a valid one.

If "(%d%d)+" is a valid pattern, what does it match? And if it isn't, is there a particular reason no error is produced?

like image 222
Bart Kiers Avatar asked Jan 27 '26 02:01

Bart Kiers


2 Answers

"(%d%d)+" is a valid pattern. It matches for example "some 45+67 text" or "some 4567+ text" and captures "45" in the first case and "67" in the second.

like image 111
Socken Puppe Avatar answered Jan 29 '26 16:01

Socken Puppe


To match two successive occurrences of a string of digits, use "(%d+)%1".

like image 25
lhf Avatar answered Jan 29 '26 14:01

lhf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!