9.7. Syntax Flag¶
\S perform ASCII-only matching instead of full Unicode matching
Case-insensitive (has Unicode support i.e. Ą and ą)
\B and case-insensitive matching dependent on the current locale
match can start in one line, and end in another:
^ - start of line,
$ - end of line
. matches also newlines (default newlines are not matched by
turns on UNICODE mode
ignores spaces (except
\s) and allows for comments in in
>>> import re >>> >>> a = re.compile(r"""\d + # the integral part ... \. # the decimal point ... \d * # some fractional digits""", re.VERBOSE) >>> >>> b = re.compile(r"\d+\.\d*")
The final piece of regex syntax that Python's regular expression engine offers is a means of setting the flags. Usually the flags are set by passing them as additional parameters when calling the re.compile() function, but sometimes it's more convenient to set them as part of the regex itself. The syntax is simply (?flags) where flags is one or more of the following:
If the flags are set this way, they should be put at the start of the regex; they match nothing, so their effect on the regex is only to set the flags.
The letters used for the flags are the same as the ones used by Perl's regex engine, which is why s is used for re.DOTALL and x is used for re.VERBOSE.
Summerfield, Mark. Programming in Python 3. Regular Expressions. Chapter: 12. Pages: 445-465. Year: 2008. Retrieved: 2021-04-11. Publisher: Addison-Wesley Professional. ISBN: 978-0-13-712929-4. URL: https://www.informit.com/articles/article.aspx?p=1278986