Using Conditional Expressions

Conditional expressions help qualify and restrict regex searches, increasing the probability of a desirable match. The vertical bar “|” symbol, meaning “OR”, places a condition on the regex to search for either one character in a string or another. Because the regex has a list of alternate choices to evaluate, this regex technique is called “alternation”. To search for either one character or another, insert a vertical bar “|” between the desired characters.

Example 1: Use “|” to alternate a search for various spellings of a string.

  • Regex:
    gray|grey
  • Matches:
    gray
    grey
  • Doesn't Match:
    GREY
    Gray

Example 2: Use “|” to alternate a search for either email or Email or EMAIL or e-mail.

  • Regex:
    email|Email|EMAIL|e-mail
  • Matches:
    email
    Email
    EMAIL
    e-mail
  • Doesn't Match:
    EmAiL
    E-Mail