Verbose Mode
It allows representing a regular expression in a more readable way. Let’s look at the below code.
Python3
import re match = re.search(r """(?P<first_two>[\d]{2}) # The first two digits - # A literal python (?P<last_three>[\d]{3}) # The last three digit """ , '25-542' , re.VERBOSE) print (match) |
Output
<_sre.SRE_Match object; span=(0, 6), match='25-542'>
The Verbose flag treats # character as a comment character and also ignores all the whitespace characters including the line break.
Python Flags to Tune the Behavior of Regular Expressions
Python offers some flags to modify the behavior of regular expression engines. Let’s discuss them below:
- Case Insensitivity
- Dot Matching Newline
- Multiline Mode
- Verbose Mode
- Debug Mode