0
\$\begingroup\$

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?

Heslacher
50.9k5 gold badges83 silver badges177 bronze badges
asked Aug 2, 2017 at 11:27
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm missing a short explanation why you need this expression and what you try to achieve? \$\endgroup\$ Commented Oct 13, 2017 at 19:47

2 Answers 2

2
\$\begingroup\$

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?

answered Nov 12, 2017 at 20:50
\$\endgroup\$
1
\$\begingroup\$
[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,} ==> +
answered Aug 11, 2017 at 14:49
\$\endgroup\$
1
  • \$\begingroup\$ would there be any speed or other benefit to using quantifiers as a "sugar" instead of explicit interval specifying? \$\endgroup\$ Commented Aug 14, 2017 at 15:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.