[Tutor] Regular expression on python
Steven D'Aprano
steve at pearwood.info
Wed Apr 15 03:02:59 CEST 2015
On Wed, Apr 15, 2015 at 12:49:26AM +0100, Alan Gauld wrote:
> New one on me. Where does one find out about verbose mode?
> I don't see it in the re docs?
>
> I see an re.X flag but while it seems to be similar in purpose
> yet it is different to your style above (no parens for example)?
I presume it is documented in the main docs, but I actually found this
in the "Python Pocket Reference" by Mark Lutz :-)
All of the regex flags have three forms:
- a numeric flag with a long name;
- the same numeric flag with a short name;
- a regular expression pattern.
So you can either do:
re.compile(pattern, flags)
or embed the flag in the pattern. The flags that I know of are:
(?i) re.I re.IGNORECASE
(?L) re.L re.LOCALE
(?M) re.M re.MULTILINE
(?s) re.S re.DOTALL
(?x) re.X re.VERBOSE
The flag can appear anywhere in the pattern and applies to the whole
pattern, but it is good practice to put them at the front, and in the
future it may be an error to put the flags elsewhere.
When provided as a separate argument, you can combine flags like this:
re.I|re.X
--
Steve
More information about the Tutor
mailing list