Lua string.match uses irregular regular expressions?

I am curious why this does not work, and I need to know why and how to get around it; I'm trying to determine if some input is a question, I'm sure string.match is what I need, but:

print(string.match("how much wood?", "(how|who|what|where|why|when).*\\?"))

returns nil. I 'm sure Lua string.match uses regular expressions to find matches in a string, since I used wildcards (.) Before with success, but maybe I don't understand all the mechanics? Does Lua need special separators in string functions? I checked my regex here , so if Lua used regular regex, it seems that the above code will return "how much wood?" .

Can any of you tell me what I'm doing wrong, what I wanted to do, or give me a good link where I can get comprehensive information on how Lua manipulation functions use regular expressions?

+6
source share
2 answers

Lua does not use regex. Lua uses Patterns that look the same but match each other.

.* will also use the latter ? input, so it doesn't work on \\? . The question icon should be excluded. Special characters are escaped with % .

 "how[^?]*%?" 

As Omri Barel said, there is no alternative operator. You probably need to use several patterns, one for each alternative word at the beginning of the sentence. Or you can use a library that supports expressions like the expression.

+11
source

According to manual, templates do not support striping.

So, while "how.*" Works, "(how|what).*" Doesnt.

And kapep is right when a question mark is swallowed .* .

There is a related question: matching Lua patterns and regular expressions .

+8
source

Source: https://habr.com/ru/post/895529/


All Articles