Simple, all the characters you put between brackets are actually a character class: a list of characters that must be accepted.
For example, [abc]
means "the letter a
, or b
, or c
" (any of them). And you can also specify ranges: [a-f]
is "any letter of a
to f
".
So, [11-14|21-24|31-34]
actually means "the digit 1
, or a digit from 1 to 4 (1-4
), or the character |
, or the digit 2
, etc.".
And how did you put the quantifier {2}
, regex accepts 2 occurrences of that (i.e., 2 characters that are 1, or a digit from 1 to 4, or |
, etc). So she accepts 44, and even ||
would be accepted (can test).
So the correct expression would be ^[1-3][1-4]$
(the first character is a digit from 1 to 3, and the second is a digit from 1 to 4).
Obs: Notice I removed the shortcut \A
because it seems redundant, since the ^
indicates the start of the string, and the \A
also (the difference is whether you use the multi-line mode, in which the ^
also considers the beginning of a line, but then you would have to choose between one or the other anyway, because both in the same expression - one right after the other - does not make sense).
Have you tested
^\A[1-3][1-4]$
?– anonimo
Perfect, it worked right and much simpler. I never did Regular Expression in my life, first time, I searched today on the subject. Thank you very much.
– CHNeves
Carlos and @anonimo - just remember what to use
^
and\A
together does not make much sense (as I explain in my answer below - I just did not detail more because it is not the focus of the question)– hkotsubo