Python: Comment regular expressions
source link: https://proinsias.github.io/til/python-comment-regular-expressions/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Francis T. O'Donovan
Senior Data Science Manager at Hospital IQ – Planet discoverer, researcher, developer, geek.
Python: Comment regular expressions
1 minute read
The book “Dive Into Python” suggests implementing comments using Verbose Regular Expressions:
Python allows you to do this with something called verbose regular expressions. A verbose regular expression is different from a compact regular expression in two ways:
- Whitespace is ignored. Spaces, tabs, and carriage returns are not matched as spaces, tabs, and carriage returns. They’re not matched at all. (If you want to match a space in a verbose regular expression, you’ll need to escape it by putting a backslash in front of it.)
- Comments are ignored. A comment in a verbose regular expression is just like a comment in Python code: it starts with a
#
character and goes until the end of the line. In this case it’s a comment within a multi-line string instead of within your source code, but it works the same way.Example:
>>> pattern = """ ^ # beginning of string M{0,4} # thousands - 0 to 4 M's (CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's), # or 500-800 (D, followed by 0 to 3 C's) (XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's), # or 50-80 (L, followed by 0 to 3 X's) (IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's), # or 5-8 (V, followed by 0 to 3 I's) $ # end of string """ >>> re.search(pattern, 'M', re.VERBOSE) 1
Source and further details here.
This method has a slight disadvantage that the caller must know that the pattern is written in a verbose format and call it accordingly.
Via softwareengineering.stackexchange.com.
Categories: til
Updated: June 25, 2022
You May Also Enjoy
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK