Regular expressions - a simple overview
Apr 24, 2023 • Erwin Timmerman
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
White space
Repetition
Position in the line
Groups
Look ahead / Look back
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.
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.
Remove manual formatting from HTML code.
This function removes all spans with styles (instead of classes) from HTML code.
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
- Older
- Newer
RECENT POSTS
- What is Regular Expression?
- Regular expressions - a simple overview
- 5 Python Tips to Supercharge Your Coding Skills
- Python Code Quality: Tools & Best Practices
- Merge Dictionaries in Python: 8 Standard Methods (with code)
- Ready to take your Python skills to the next level?
- Python 3.12 Preview: Ever Better Error Messages
- 7 Python Tools All Data Scientists Should Know How to Use
- Python Code Quality
- Developing an API with FastAPI and GraphQL
- All posts ...