4

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.

MAKZ
  • 165
  • 14

2 Answers2

4

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
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
4

In ^([0-9A-Za-z]{5})+$ you're saying any number or letter 5 characters long 1 or more times. The + is on the entire group (whatever's inside the parentheses) and the {5} is on the [0-9A-Za-z]

Your second example has a no backtrack clause {5}+, which is different than (stuff{5})+

JNYRanger
  • 6,829
  • 12
  • 53
  • 81