As part of my validation I have the following regex expression:
^
# Prevent 3+ consecutive letters: 'wheee'
(?!\w*([a-zA-Z])\g{-1}{2,})
[a-zA-Z](?:[a-zA-Z]|'(?!')){0,}
# Words or '&' are space or hyphen separated
(?:
[ -]
# Prevent 3+ consecutive letters: 'wheee' (for rest of words)
(?!\w*([a-zA-Z])\g{-1}{2,})
(?:[a-zA-Z]|'(?!')){1,}
|
[ -]
&
)*$
I was wondering the effectiveness of the expression and/or possible pitfalls?
-
1\$\begingroup\$ I'm missing a short explanation why you need this expression and what you try to achieve? \$\endgroup\$mheinzerling– mheinzerling2017年10月13日 19:47:15 +00:00Commented Oct 13, 2017 at 19:47
2 Answers 2
One possible pitfall is that several celebrities cannot pass your "validation" code. Do you really want Renée Zellweger and Goran Ivanišević being mad at you?
[a-zA-Z]
You use both lowercase and uppercase everywhere. This can possibly be symplified with (inline) modifiers:
(?i)[a-z]
Personally I use quantifiers as a "sugar" instead of explicit interval specifying:
{0,} ==> *
{1,} ==> +
-
\$\begingroup\$ would there be any speed or other benefit to using quantifiers as a "sugar" instead of explicit interval specifying? \$\endgroup\$Vaishal Patel– Vaishal Patel2017年08月14日 15:10:49 +00:00Commented Aug 14, 2017 at 15:10