What is a Regular Expression?

A regular expression, also known as “regex”, is useful for complex searches.

A regular expression, also known as “regex”, is a search pattern. Regex is useful when the search is more complicated than just needing to find a specific word.

For example, let’s say a computer program has made a big file that lists everything it has done, and each line starts with a date like “2019-04-01”. If we want search for lines that start with a certain date, we might want to use regex:

  • If we just need everything that happened in April, we can search for “2019-04”. We could do a plain, non-regex search for “2019-04”. But “2019-04” also happens to be a simple regular expression.
  • If we need to find any line from April or May, we can use the regular expression “2019-0(4|5)”. The “(4|5)” part just means that either or 4 or five is OK in that position.
  • If the computer program was inconsistent, and the format is “2019-04-01” some times and “2019_04_01” other times, we can use the regular expression “2019.04.01”. The periods are wild cards.

Regular expressions can get more complicated than that, but that’s the general idea.

There are multiple formats for regular expressions. So some times you’ll see slashes around the regular expressions, or a regular expression that works in one program won’t work in another.

Stephen Cole Kleene
Stephen Cole Kleene, who helped invent regular expressions in 1951.

Leave a Reply