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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With