A regular expression is a sequence of characters that defines a search pattern, typically used for finding or replacing specific text in strings.
n this guide I’ll break down regex into simple steps, showing you how to create patterns, enclose them correctly, and use flags to customize searches 🙌
Table of Contents
Let’s dive in!
What is a regular expression
A regular expression (often called “regex”) is like a search command that helps you find patterns in text.
Instead of manually looking for certain words, numbers, or characters, you can create a regex pattern that does the job for you 💪
For example, if you want to find every instance of a phone number, instead of typing each possible number, you can create a pattern that finds all of them for you at once.
Regular expressions are superduper handy for tasks like replacing parts of a text, validating input, or extracting data.
While they might seem a bit tricky at first, trust me, once you get the hang of it (or just let ChatGPT generate it for you), they save a lot of time!
Example of a regular expression
Here is an example of a regular expression where we replaced all digits in the text by X
.
replace(My phone number is 2-9993-2337-13;/\d+/g;X)
= My phone number is X-XXXX-XXXX-XX
The regular expression /\d+/g
consists of 3 parts;
/ /
the forward slashes let Make.com know where the pattern starts and ends.\d+
is the pattern we’re looking for. In this case it stands for digits (0-9) of any length like 2, 13, or 9993 in the example above.g
is the “flag” that says we want all digits, not just the first occurrence.
That might sound a bit complicated, but in the next bit we’re going over each part.
Writing regular expression
Step #1: Create pattern
First, you have to create a pattern to match whatever you’re searching for.
If you simply want to find the word cats
in the sentence I have 12 cats that love me
, then you pattern is cats
But if you would want to match all digits in that text, you need to use \d+
and it will return 12
.
And from there, it quickly gets very complex, and not a soul on earth knows those patterns out of their head.
Luckily, there are regex generators that allow you to create a pattern by telling which part of the text you’re interested in, but I find it even easier to use this prompt in ChatGPT:
I have the text below and I want to match all digits with a regular expression. Generate it for me.
“I have 12 cats that love me”
You can use the following regular expression to match all digits in the text:
\d+
This will find all sequences of digits in the text. For example, in “I have 12 cats that love me,” it will match “12”.
Another example, I asked:
What’s the regex for extracting URLs from text?
Here’s a regex pattern you can use to extract URLs from text:
\b(?:https?|ftp):\/\/[^\s/$.?#].[^\s]*\b
This pattern should work for most common URL formats.
But be sure to test your regex, sometimes ChatGPT gets a bit too creative.
So now you know how to create the most complex regular expressions in an instant 🔥
Step #2: Enclose regular expression
All regular expressions need to be enclosed by forward slashes /../
So if we want to use \d+
to find all digits, you need to enclose it like this: /\d+/
Step #3: Append flag
In regular expressions, flags are special characters appended to the end of the regex pattern to modify its behavior 🙃
For example, the flag in the regular expression /dog/g
is g
and will now find all instances of “dog” and not just the first one.
Here’s what the common flags do:
g
(global): Matches the pattern multiple times in the string, not just the first occurrence.i
(case-insensitive): Makes the match case-insensitive, so “A” will match “a”.m
(multiline): Changes how the ^ and $ anchors work, making them match the start and end of each line within a string rather than just the start and end of the whole string.
You can also combine all flags like /dog/gim
so it applies global, case-insensitive, and multiline matching.
If you don’t append any flags, the regex will use default behavior. For instance, /dog/
will match “dog” only once and is case-sensitive.
Some examples;
/dog/g
matches all instances of “dog” in the string./dog/i
matches “dog” regardless of case./dog/m
changes how anchors like ^ and $ behave./dog/gi
matches all instances of “dog” in the string regardless of case./dog/
matches the first instance of “dog” which is case sensitive
Got it? 😄
Using your regular expression
Inside a function
Here is an example of how we use a regular expression in the replace() function to replace all digits in a text by X
.
replace(My phone number is 9993-2937-123;/\d+/g;X)
= My phone number is XXXX-XXXXX-XXX
Matching a pattern
Want to get something out of a text? Then the text parser module is your friend.
Let’s say I want to extract the email address out of this text;
To do that, we’re going to Match a pattern with the Text parser module.
The pattern in the top is the regular expression for an email address, surrounded by brackets.
I’ve then set the global match to No
so it only gets the first email address in case there are multiple.
And this will give us only the email address in the text back, which we can now use for something else.
Handy, right?
And you can do this with any kind of information;
- Extract order numbers
- Extract phone numbers
- Extract URLs
Go crazy!
Conclusion
Regular expressions are a bit techy, but they can be very handy.
And especially with tools like ChatGPT to help you generate the pattern, it’s superduper easy.
Any questions? Let me know in the comments down below 😄