Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua string.match uses irregular regular expressions?

Tags:

regex

lua

I'm curious why this doesn't work, and need to know why/how to work around it; I'm trying to detect whether some input is a question, I'm pretty sure string.match is what I need, but:

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

returns nil. I'm pretty sure Lua's string.match uses regular expressions to find matches in a string, as I've used wildcards (.) before with success, but maybe I don't understand all the mechanics? Does Lua require special delimiters in its string functions? I've tested my regular expression here, so if Lua used regular regular expressions, it seems like the above code would return "how much wood?".

Can any of you tell me what I'm doing wrong, what I mean to do, or point me to a good reference where I can get comprehensive information about how Lua's string manipulation functions utilize regular expressions?

like image 918
Uronym Avatar asked Aug 21 '11 12:08

Uronym


People also ask

How do I use string match in Lua?

> = string. match("foo 123 bar", '%d%d%d') -- %d matches a digit 123 > = string. match("text with an Uppercase letter", '%u') -- %u matches an uppercase letter U. Making the letter after the % uppercase inverts the class, so %D will match all non-digit characters.

Does Lua use regex?

They all are based on patterns. Unlike several other scripting languages, Lua does not use POSIX regular expressions (regexp) for pattern matching.

Which function we use to match pattern in regular expression?

Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() . Executes a search for a match in a string. It returns an array of information or null on a mismatch.

What does GSUB do in Lua?

gsub (s, pattern, repl [, n]) Returns a copy of s in which all (or the first n , if given) occurrences of the pattern have been replaced by a replacement string specified by repl , which can be a string, a table, or a function. gsub also returns, as its second value, the total number of matches that occurred.


2 Answers

Lua doesn't use regex. Lua uses Patterns, which look similar but match different input.

.* will also consume the last ? of the input, so it fails on \\?. The question mark should be excluded. Special characters are escaped with %.

"how[^?]*%?"

As Omri Barel said, there's no alternation operator. You probably need to use multiple patterns, one for each alternative word at the beginning of the sentence. Or you could use a library that supports regex like expressions.

like image 184
kapex Avatar answered Nov 14 '22 11:11

kapex


According to the manual, patterns don't support alternation.

So while "how.*" works, "(how|what).*" doesnt.

And kapep is right about the question mark being swallowed by the .*.

There's a related question: Lua pattern matching vs. regular expressions.

like image 33
Omri Barel Avatar answered Nov 14 '22 10:11

Omri Barel