Regular expressions - a simple overview

Apr 24, 2023 • Erwin Timmerman

Regular expressions - a simple overview

If you want to find/replace text automatically, regular expressions can be of great help to search for specific parts or patterns in a text. Here are some examples to get you started.

Character sets

No alt text provided for this image

White space

No alt text provided for this image

Repetition

No alt text provided for this image

Position in the line

No alt text provided for this image

Groups

No alt text provided for this image

Look ahead / Look back

No alt text provided for this image

Practical examples

Regular expressions can form equations that may look quite complex, but when you break them down, it is easier to understand them. Here are a few examples that show the power of regular expressions.

Find numbers that have 3 or more repeated digits, such as 3111 or 59992.

You can use this, for example, to check if someone does not enter a PIN that is too simple.

No alt text provided for this image

Replace the mm-dd-yy date format with the dd-mm-yy date format, and vice versa.

This function replaces, for example, 9-17-’69 with 17-9-’69, and 17/05/1973 with 05-17-1973.

No alt text provided for this image

Remove manual formatting from HTML code.

This function removes all spans with styles (instead of classes) from HTML code.

No alt text provided for this image

Flavors

There are several flavors of regular expressions. Most of these examples work on all of them, but some might need a different syntax in specific programming languages. If an example does not work as expected, look at the documentation of your specific application.

Want to know more?

For a more detailed explanation of regular expressions, have a look at these websites:

  • https://www.regular-expressions.info/quickstart.html
  • https://www.rexegg.com/regex-quickstart.html