Original link: https://www.kingname.info/2022/06/20/readable-re/
Regular expressions are powerful, but they are written like an emoji. When I look at the expression I wrote in a month, I don’t even remember what it means. For example the following:
1 |
pattern = r"((?:\(\s*)?[AZ]*H\d+[az]*(?:\s*\+\s*[AZ]*H\d+[az]*)* (?:\s*[\):+])?)(.*?)(?=(?:\(\s*)?[AZ]*H\d+[az]*(?:\s* \+\s*[AZ]*H\d+[az]*)*(?:\s*[\):+])?(?![^\w\s])|$)" |
Is there any way to improve the readability of regular expressions? We know that one of the ways to improve code readability is to write comments, so can regular expressions write comments?
For example for the following sentence:
1 |
msg = 'My name is Qingnan, my password is: 123kingname456, please keep it secret. ' |
I want to extract the password 123kingname456
, so my regular expression might be like this:
1 |
pattern = ':(.*?),' |
Can I write it like this:
1 |
pattern = ''' |
This way, the writing is much clearer, and what each part does is clear.
But obviously nothing can be extracted by direct use, as shown in the following figure:
But when I was browsing the Python regular expression documentation today, I found a good thing:
Using it, you can make your regular expressions have comments, as shown in the following image:
re.VERBOSE
can also be referred to as re.X
for short, as shown in the following figure:
The complex regular expression at the beginning of this article will become more readable after using comments:
1 |
pattern = r""" |
This article is reprinted from: https://www.kingname.info/2022/06/20/readable-re/
This site is for inclusion only, and the copyright belongs to the original author.