Understanding regular expressions with re(gex)? modules
Scripting and automation tasks often need to extract particular portions of text from input data or modify them from one format to another. Regular Expressions comes in handy for such cases. It can be described as a mini-programming language in itself, rich in features to precisely define and manipulate patterns. Here's some loose fitting analogies:
- Anchors ➔ adding if conditions
- Alternation ➔ Conditional OR
- a(b|c)d = abd|acd ➔ a(b+c)d = abd+acd
- Quantifiers ➔ string repetition and range operators
- Dot metacharacter + quantifiers + alternation ➔ Conditional AND
- Capture group and backreference ➔ variable
- Subexpression call ➔ function
- Character class ➔ sets
- Lookarounds ➔ custom conditionals
- Flags ➔ like command line options
This workshop aims to cover from basics to advanced levels, using plenty of examples and exercises to go through the features step by step. Regular expressions (and its cousin - shell globs/wildcards) are prevalent across command line tools, editors, IDEs, programming languages, etc. So, a deeper understanding would be beneficial outside of Python as well. However, beginners are often tripped by syntax and feature differences across implementations. It becomes necessary to at least have a handy reference sheet specific to the tool you are using. Python itself has subtle variations between, say version 3.5 and 3.7. Apart from re module which is part of standard library, the regex module will also be discussed, which is a popular alternative when more powerful features are needed.
Basic to intermediate level Python knowledge, should know concepts like string formats, string methods, list comprehension and conditional expressions.
Prior experience with regular expressions is not necessary.
Module to be installed: https://pypi.org/project/regex
Python version: 3.7+
Sundeep Agarwal is a freelance trainer, author and mentor. His previous experience includes working as a Design Engineer at Analog Devices for more than 5 years. His writing includes tutorials and books primarily focused on Linux command line, text processing, scripting languages and curated lists. He has also been a technical reviewer for Command Line Fundamentals book and video course published by Packt.