Harnessing the Power of Regex101: A Comprehensive Guide
Today we’re exploring the magic of regular expressions (regex) using the incredibly useful tool, Regex101.
Regex101 is an online regex tester and debugger, allowing you to create, test, and debug your regex patterns in real-time. It provides extensive explanations for your regex and supports JavaScript, Python, PHP, and more.
Basics of Regex101
When you first open Regex101, you’ll see four main sections:
- Regular Expression: This is where you type your regular expression.
- Test String: Here you type the text you want to test against your regular expression.
- Match Information: This section shows the match information for your regular expression against the test string.
- Explanation: Here, an explanation of your regex is provided.
Utilizing Regex101
Let’s start with a simple regex for matching an email address:
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
Enter this pattern in the Regular Expression field and test it with various strings in the Test String field. The Match Information panel will light up with matches, if any. The Explanation panel will break down each component of the regex for you.
Diving Deeper into Regex
Now, let’s explore a more complex scenario. Suppose you want to extract all the URLs from a block of text. Here’s a regex pattern that could be used:
\b(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?\b
Enter this regex in the Regular Expression field and test it with a text block containing URLs in the Test String field. The Match Information panel will highlight all the URLs, and the Explanation panel will provide a detailed breakdown.
Using Substitution in Regex101
Regex101 also allows for substitutions. This is a powerful feature that replaces the matched parts of your test string based on your substitution pattern.
For example, let’s say you want to redact all email addresses in your text. You can use the substitution feature to replace all matches with [REDACTED]. After writing your regex for email addresses, enter “[REDACTED]” in the Substitution field.
Using Substitutions in Regex101 to Redact Email Addresses
Let’s consider the following block of text:
Hello, my name is John Doe. You can contact me at [email protected].
My friend, Jane, also wants to hear from you. Her email is [email protected].
This is our dummy data, which contains two email addresses we want to redact.
Creating the Regex Pattern
To match an email address, we’ll use the following regex pattern:
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
Enter this pattern in the Regular Expression field of Regex101.
Using the Substitution Feature
In the Substitution field, enter the word “[REDACTED]”. This is what we’ll replace the email addresses with.
Results
After entering the regex and substitution, our block of text in the Test String field is transformed into:
Hello, my name is John Doe. You can contact me at [REDACTED].
My friend, Jane, also wants to hear from you. Her email is [REDACTED].
Saving and Sharing your Regex
One of the best features of Regex101 is that you can save and share your regex. This can be very useful when you want to document the regex you have created and share it with others. Click on the ‘Save Regex’ button, it will generate a URL that you can share.
Regular expressions are powerful and flexible. Though they may seem complex at first, with practice, you can master them. Regex101 is an excellent tool that can aid you in this learning process by providing real-time feedback and detailed explanations.
Buy Me a Coffee