Debug Mode
The re.DEBUG flag provides debugging information while compiling a regular expression. Let’s have a look at the below code.
Python3
import re match = re.search(r '(?P<first_two>[\d]{2})-(?P<last_three>[\d]{3})' ,\ '25-542' , re.DEBUG) print (match) |
Output
SUBPATTERN 1 0 0 MAX_REPEAT 2 2 IN CATEGORY CATEGORY_DIGIT LITERAL 45 SUBPATTERN 2 0 0 MAX_REPEAT 3 3 IN CATEGORY CATEGORY_DIGIT <_sre.SRE_Match object; span=(0, 6), match='25-542'>
Here, you have seen different types of flags that can slightly change the behavior of a regular expression engine. You can also use multiple flags at the same time by using a bitwise OR (|) operator.
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