Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE2 (PHP >=7.3)

/
([0-7]\d{2}(" "|-|\.)?\d{2}(?(2)(" "|-|\.))\d{4})
/
gm

Description

([0-7]\d{2}(" "|-|.)?\d{2}(?(2)(" "|-|.))\d{4})

  • Finds the first number as 0-7 then matches the next two digits
  • Will check what delimitator (group 2) if any is used
  • finds two digits
  • if a delimitator is used - if it is then use one here, if not then don't (prevents US based zip code + 4 from getting caught up)
  • finds four digits

Additional tweaks can be made to make it more efficient - this works for me for now.

Submitted by ACCS - 2 years ago