I'm looking for amount of repetitions of symbols in Lua pattern setup. I try to check amount of symbols in a string. As I read in manual, Even with character classes this is still very limiting, because we can only match strings with a fixed length.
To solve this, patterns support these four repetition operators:
- '*' Match the previous character (or class) zero or more times, as many times as possible.
- '+' Match the previous character (or class) one or more times, as many times as possible.
- '-' Match the previous character (or class) zero or more times, as few times as possible.
- '?' Make the previous character (or class) optional.
So, no information about Braces {}
e.g.,
{1,10}; {1,}; {10};
doesn't work.
local np = '1'
local a = np:match('^[a-zA-Z0-9_]{1}$' )
returns np = nil
.
local np = '1{1}'
local a = np:match('^[a-zA-Z0-9_]{1}$' )
returns np = '1{1}'
:)
This url says that no such magic symbols:
Some characters, called magic characters, have special meanings when used in a pattern. The magic characters are
( ) . % + - * ? [ ^ $
Curly brackets do work only as simple text and no more. Am I right? What is the best way to avoid this 'bug'?
It is possible to read usual usage of braces, for instance, here.
\d{2,}
is%d%d+
). Also you can use Lua rex pcre library. – Snappnp:match('^'..('[%w_]'):rep(k)..'$')
– Wulf#np==k and not np:find'[^%w_]'
– Wulf> require "rex_pcre" > return rex_pcre.new("^[a-zA-Z0-9_]{2}$"):exec("12")
. – Moralez