Why is the bracket required here?

1. ^([0-9A-Za-z]{5})+$

against

2. ^[a-zA-Z0-9]{5}+$

My intention is to match any string of length nso that it is na multiple 5. Check here: https://regex101.com/r/sS6rW8/1 .

Please clarify why case 1 matches the line, while case 2 does not.

+4
source share
2 answers

Because it {n}+does not mean what you think. In the PCRE syntax, this turns {n}into a possessive quantifier . In other words, a{5}+matches (?>a{5}). This is similar to the second +in the expression a++, which is the same as using an atomic group (?>a+) .

{n}, {min,max}. , a{2,5}+ (?>a{2,5}).

:

^(a{1,2})(ab)    will match  aab -> $1 is "a", $2 is "ab"
^(a{1,2}+)(ab)   won't match aab -> $1 consumes "aa" possessively and $2 can't match
+4

^([0-9A-Za-z]{5})+$ 5 1 . + ( ), {5} [0-9A-Za-z]

backtrack {5}+, (stuff{5})+

+4

All Articles