S
S
Sergey Pugovkin2018-09-06 02:23:36
Regular Expressions
Sergey Pugovkin, 2018-09-06 02:23:36

How to specify the number of characters in a regular expression in lua?

local string = "qw.erty"
return string:match("([a-z]{2})%.")

Returns nil
There is not a word about this in the manuals.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2018-09-06
@Driver86

For starters, it's best not to use a word stringfor the variable name, as it's a whole library for working with strings. You block access to it.
Lua does not have full-fledged regular expressions, because Lua was conceived as the lightest possible language. Instead, the so-called. "pattern match". So that {2}doesn't work. It's just that in most cases modifiers are enough. + - * ?
In your case, it will do:

local str = "qw.erty"
return str:match("([a-z][a-z])%.")

Or even:
local str = "qw.erty"
return str:match("(%l%l)%.")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question