-1
\$\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?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I'm missing a short explanation why you need this expression and what you try to achieve? \$\endgroup\$ Oct 13, 2017 at 19:47

2 Answers 2

1
\$\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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.