Introduction
Using regular expressions remains one of the most underrated techniques when it comes to text manipulation and searching. Due to its uncommon syntax, at first glance it may seem arcane to many first timers (and we can’t blame them!).
However, with a bunch of helpful tools and some examples to familiarize yourself with the technique, regex becomes a lot more intuitive and opens up a lot of possibilities for manipulating text. And that’s exactly what we aim to share in this blog post.
Note: We will be referencing the UltraEdit Masterclass on Regex for this blog. Feel free to read this blog alongside the webinar replay.
Why use regular expressions
Regular expressions give you an alternative way to filter and search for specific strings using pattern matching and text manipulation. It is a versatile tool that allows you to define complex search patterns to match sequences of characters in strings.
It is particularly useful if you don’t want to define each string exactly but you know that they are following a certain pattern or format. For example, an email address or a phone number which would have a “[email protected]” or “+09-87654321” format, respectively.
To summarize, regular expressions provide a pattern searching technique primarily used for:
- Validating the existence of text
- Validating the format of text
- Finding and extracting text
This flexibility makes regex ideal for complex search operations that go beyond the capabilities of simple string search functions. For instance, regex can validate data like email addresses, extract URLs from text, reformat existing text, or replace specific patterns with new text—which works exceptionally well for dealing with input data.
Difference between regex and regular string search/code
Compared to normal search and filter operations, regex offers greater flexibility and complexity, enabling you to define intricate patterns with optional lements, repetitions, and character classes.
However, this power comes with trade-offs in terms of performance and readability. Regex can be slower, especially with complex patterns and large datasets, and its syntax can be challenging to understand.
While regex is best suited for tasks requiring advanced pattern matching, normal search and filter operations are more intuitive and efficient for traightforward, exact matches. The trick is to determine where and how you should use regex vs regular string matching.
Protip: To understand what each part of the regex does, experiment with a regex tester.
The regex101 web app. This tool can be used to describe portions of a regex.
Example 1: Extracting Dates using regular expressions in Python
In this example, we use both regex and manual string matching to extract dates from a sentence.
import re text = "The event is scheduled for 2023-10-01 and 2023-11-15." pattern = r"\b\d{4}-\d{2}-\d{2}\b" dates = re.findall(pattern, text) print("Dates found:", dates)
Using regex to match sections of text following the yyyy-mm-dd pattern.
text = "The event is scheduled for 2023-10-01 and 2023-11-15." words = text.split() dates = [] for word in words: if len(word) == 10 and word[4] == '-' and word[7] == '-': try: year, month, day = map(int, word.split('-')) dates.append(word) except ValueError: continue print("Dates found:", dates)
Using manual string search. This brute force method parses through the entire line of text and checks if it follows the date format—not the most optimal solution, but does work.
Example 2: Input Validation (Email) with regex in Python
In this example, we use both regex and manual string matching to try and validate an email address.
import re email = "[email protected]" pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" if re.match(pattern, email): print("Valid email address") else: print("Invalid email address")
This uses a regex to match where the username and the domain part of the email are.
email = "[email protected]" if "@" in email and "." in email.split("@")[1]: print("Valid email address") else: print("Invalid email address")/code>
This uses string splitting and if statements to validate emails. Offers a simpler solution than the last example because of the predictable structure of email addresses.
Summary:
Both examples show how regex and regular string matching can achieve the same result. However, it is important to first check what type of data you are processing and what your intended output is. Both methods will work but one may be more complex than the other.
The first example with the email shows how a regex expression can easily replace multiple lines and string operations. On the other hand, the second example with the email validation shows that regular string operations can sometimes still be less complex and easier to use versus regex.
More examples of regex + webinar demo
Here are more examples that showcase scenarios of using regex. It uses real files and common formats of text that you can usually encounter on a day to day basis. This should paint a clearer picture of where you can utilize regex to its fullest potential.
The following presentation showcases a tool called UltraEdit (UE), a handy text editor that includes built-in support for regex search and replace. It’s a versatile tool for developers and can be used for editing code, writing scripts, and performing complex text manipulations using regex—especially on large files.
If you are interested in seeing more examples of how regex is used in the real world, we recommend watching the “UltraEdit Masterclass: The Regex Toolkit You’ll Actually Use” webinar focused on exploring Perl Compatible Regular Expressions with practical, real-world examples.
If you want to try these examples out for yourself, here are the sample files used in the webinar. Here is a rundown of the session:
- Find and Replace Operations
- Regex Find & Replace: Modifying HTML tags
- Regex Find & Replace: Cleaning up data format
- Regex Scripts: Automate repetitive tasks using scripts
- Practical Use Cases: parsing logs
- Practical Use Cases: data validation
- Practical Use Cases: text manipulation
- More examples using Perl-style regex
Example 3: Pulling dates and contact numbers from text using UltraEdit
This one uses a similar regex from the first two examples on a block of text, showing how flexible it can be with varying types of input data. Watch it in action.
\d{2}\/\d{2}\/\d{4}
Regex for finding dates in the text
\$?\d{1,3}(?:\d{3})*(?:\,\d{2})?
Regex for matching currency values, specifically formatted as US dollars.
Example 4: Removing HTML tags, attributes, and values (<>,</>) from text in UltraEdit
<[^>|+>
Simple regex that is used to replace all instances of text in between carets (e.g. < p> text < p/>) to turn HTML into plain text. The screenshot shows the regex101 web app integrated as a window inside the UE editor.
Example 5: Matching .csv fields and swapping their positions
For Find: ^((?:[^,]+?,){1})([^,]+?,) For replace/swap: \2\1
These two regexes find the first two fields in a .csv file and swaps their positions. Uses regex Find and Replace.
Summary:
Example 3 shows regex’s adaptability to different input types as you can extract dates and contact numbers regardless of the length and structure of the text. Example 4 focuses on removing HTML tags to convert HTML content into plain text. And lastly, Example 5 involves finding and swapping positions of fields in a CSV file, highlighting regex’s capability in data reformatting and organization.
The last three examples show how flexible regex operations can be. It shows how when set up properly, it can step through blocks of unstructured data and accurately find what you are looking for.
Other common regex expressions shared in the webinar.
Other handy tools for dealing with regex:
- Regx101 (Regex tester)
- Regexr (Regex tester)
- Getting Started with Perl regex in UE
- UE’s Regular Expression Builder
- Regex cheat sheet
- Fixing greedy regex
- UE-flavored regex cheat sheet
Conclusion
In conclusion, regular expressions are a powerful tool that can greatly enhance your ability to manipulate and analyze text data.
From simple search and replace tasks to complex data validation and parsing, regex offer a versatile solution for a wide range of challenges. Whether you’re a developer looking to streamline your code or a data analyst seeking to extract meaningful insights from raw data, mastering regex can significantly boost your productivity and efficiency.
As you continue to explore the capabilities of regex, remember that practice and experimentation are key to unlocking its full potential. For those interested in diving deeper, consider watching the webinar for practical, real-world examples and expert insights. And for beginners, don’t miss the section about using AI and LLM models to create regexes for you!
And if you’re working with text and you’d like to experiment with regex in more detail, learn more about UltraEdit and use it to increase your productivity.
Download UltraEdit for a free 30 day full feature trial












Great article! Another useful tool for testing regular expressions and visualizing patterns is Pythonium