Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is bracket mandatory here?

Tags:

regex

pcre

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

vs

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

My intention is to match any string of length n such that n is a multiple of 5. Check here : https://regex101.com/r/sS6rW8/1.

Please elaborate why case 1 matches the string whereas case 2 doesnot.

like image 825
MAKZ Avatar asked Feb 11 '23 19:02

MAKZ


1 Answers

Because {n}+ doesn't mean what you think it does. In PCRE syntax, this turns {n} into a possessive quantifier. In other words, a{5}+ is the same as (?>a{5}). It's like the second + in the expression a++, which is the same as using an atomic group (?>a+).

This has no use with a fixed-length {n} but is more meaningful when used with {min,max}. So, a{2,5}+ is equivalent to (?>a{2,5}).

As a simple example, consider these patterns:

^(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
like image 84
Lucas Trzesniewski Avatar answered Feb 19 '23 14:02

Lucas Trzesniewski