Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
^(?![\x20\x27\x2D]|.*?[\p{Lu}]{2}|.*?[\x20]{2}|.*?[\x27]{2}|.*?[\x2D]{2}|.*?(\x20\x2D|\x2D\x20)|.*?(\x27\x2D|\x2D\x27)|.*?(\x27[\w]+\x27(\x20|$)))[\p{L}\x20\x27\x2D]+$(?<![\x20\x2D])
/
gm

Description

Validates personal names.

Conditions:

  1. Must contain only letters (any unicode letter), spaces, apostrophes and hyphens
  2. Must not start with a space, an apostrophe or a hyphen
  3. Must not contain consecutive uppercase letters
  4. Must not contain consecutive spaces
  5. Must not contain consecutive apostrophes
  6. Must not contain consecutive hyphens
  7. Must not contain a space before or after a hyphen
  8. Must not contain an apostrophe before or after a hyphen
  9. Must not contain enclosing apostrophes ('single quote' style)
  10. Must not end with a space or a hyphen
  11. Must contain at least one character
Submitted by Mattias Larsson - 8 years ago