Searching with Wildcards

In the previous examples, regular expressions are constructed with literal characters that match themselves. There are other characters in regex syntax that match in a more generalized way. These are called “metacharacters”. Metacharacters do not match themselves, but rather perform a specific task when used in a regular expression. One such metacharacter is the dot “.”, or wildcard. When used in a regular expression, the wildcard can match any single character.

Using the wildcard to match any character.

Example 1: Search for the string “ubject”.

  • Regex:
    .ubject:
  • Matches:
    Subject:
    subject:
    Fubject:
  • Doesn't Match:
    Subject
    subject

Example 2: Use three dots “...” to search for any three characters within a string.

  • Regex:
    t...s
  • Matches:
    trees
    tEENs
    t345s
    t-4-s
  • Doesn't Match:
    Trees
    twentys
    t1234s

Example 3: Use several wildcards to match characters throughout a string.

  • Regex:
    .a.a.a
  • Matches:
    Canada
    alabama
    banana
    3a4a5a
  • Doesn't Match:
    aaa