Search
Menu

In this blog, we explain to you what regular expression is and how it works. So that you too can take advantage of it later.

What is regular expression?

A regular expression, often called "regex," is a string of characters that describes a pattern. With regex, you can find and use specific patterns.

It is like an advanced search function, where you detail what you are looking for, such as certain words or numbers.

For example, you can use a regular expression to filter spam, format text, or analyze data.

How does regular expression work?

Regular expressions work by comparing the text to the pattern they describe. If the text matches the pattern, the expression is considered "true."

Regular expressions use a series of special characters to describe patterns

A simple example. Suppose you want to find all the numbers in a large text file. Then you can use the regular expression below:

[0-9]+

  • The [0-9] represents a string of digits.
  • The + indicates that there is similarity to be with one or more of the previous element

Thus, according to this regular expression, all the sequences below satisfy the regex. Every digit in the text file is now found.


0000 
112233 
1234567890 

Examples of applications

As a marketer, you can work a lot faster and more efficiently by using regular expression at the right times. For example:

  • Finding all pages on the website that contain certain keywords
  • Easily filter all valid email addresses from a file
  • Checking the performance of a page group in Google Search Console
  • Filtering data reports so that all irrelevant countries are excluded
  • Filtering your report in Google Analytics by specific countries
  • Firing a trigger on certain pages via Google Tag Manager

And much more!

Creating your own regular expression

Setting up a regular expression can be quite a puzzle. This is because within the regular expression, you use different characters and characters to establish patterns.

In the table below, we have listed the most commonly used regex characters, including examples.

Mark Meaning Example 
*Corresponds to zero or more of the previous element 
^Indicates the beginning of a line or string^abc corresponds to "abc" at the beginning of a string
$Indicates the end of a line or stringabc$ corresponds to "abc" at the end of a string
. Matches any character except a new linea.b corresponds to "acb," "aab," "a3b," etc.
+Corresponds to one or more of the previous elementa+ corresponds to "a", "aa", "aaa", etc.
?Makes the previous character optionalab?c corresponds to "ac" or "abc"
 Used to take the next character literally. (For example . literally means a period)a.b corresponds to "a.b"
[]Matches any character in the sequence[abc] corresponds to "a," "b," or "c"
[^]Matches any character not in the sequence[^abc] matches any character except "a", "b", or "c"
()Groups partial expressions(abc) treats "abc" as one unit
{n}Corresponds to exactly n times of the preceding charactera{2} corresponds to "aa"
{n,}Matches n or more of the preceding charactera{2,} corresponds to "aa", "aaa", "aaaa", etc.
{n,m}Corresponds to n to m times of the preceding signa{2,4} corresponds to "aa", "aaa", or "aaaa"

Outsourcing the writing of the regex

When setting up regex, there are endless possibilities. This also often makes it difficult to write a regular expression that works exactly the way you want it to.

You can already make it a lot easier on yourself by:

  • Deploy a regex generator
  • Having AI compile your regex
  • Asking a developer to write the code

Tip: Getting started with AI or a generator? Then start with simple regular expressions. Once you've mastered the basics, you can start creating more complex regular expressions.

Testing your regular expression

If all goes well, you have now written your regular expression yourself, used a generator, deployed AI or received the regex from a coder.

Then it's time to test if it works! To do this, you can use a RegEx tester. For example, Regex101.com.

Add the regular expression you prepared to the bar at the top, then add several URLs to test if the URL matches the RegEx.

Also test a few URLs that may not match the RegEx. This will ensure that you don't include more than you intend.

Let's look again at the example from earlier: [0-9]+. With this, we want to find all the digits in a given text file. To test whether the regex does what it is supposed to do, enter the regex at the top. Then add some combinations. Add both right and wrong strings.

Regex compiling example

In this case, the regular expression does exactly what we want. All the numbers produce a match.

Of course, we can make this a bit more complicated. Suppose we only want strings of digits consisting of 4 digits, and no other characters are allowed. Then the test looks like this:

Set up regular expression

See! Only the digit sets with 4 digits, and no other characters, yield a match.

Getting Started with Regex

You now know exactly what regular expression is, how regex works and how to create your own rule. Time to get started and put your newly acquired knowledge into practice. Here are some final tips:

  • Start with the basics and keep it easy.
  • Use online practice tools and resources.
  • Make sure you understand the most common signs.
  • Build complex patterns gradually by combining simple patterns.
  • Test each regular expression thoroughly.
  • Practice, practice and practice again.

Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

Most frequently asked questions about this blog