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: 

  1. Case Insensitivity
  2. Dot Matching Newline
  3. Multiline Mode
  4. Verbose Mode
  5. Debug Mode

Case Insensitivity

The re.IGNORECASE allows the regular expression to become case-insensitive. Here, the match is returned based on the case of the provided string, not the string in the regular expression.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import re
  
  
match = re.search(r'geeksforgeeks', 'GeeksforGeeks',re.IGNORECASE)
print(match)

chevron_right


Output

<_sre.SRE_Match object; span=(0, 13), match='GeeksforGeeks'>

Dot Matching Newline

By using re.DOTALL flag, you can modify the behavior of dot (.) character to match the newline character apart from other characters. Before using the DOTALL flag, let’s look into how regular engine responds to the newline character.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import re
  
  
match = re.search(r'.+', 'Hello,\nGeeks')
print(match)

chevron_right


Output



<_sre.SRE_Match object; span=(0, 6), match='Hello,'>

Here, the regular expression matches one or more characters (‘. +’). At the time when the engine reaches the newline character, it stops, because the dot character doesn’t match the line breaks. Let’s look into the code that makes use of the DOTALL flag.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import re
  
  
match = re.search(r'.+','Hello,\nGeeks', re.DOTALL)
print(match)

chevron_right


Output:

<_sre.SRE_Match object; span=(0, 12), match=’Hello,\nGeeks’>

Multiline mode

With the Multiline flag, you can match against the beginning and the end of any line within the string. If we look into the ^ character, it will only match against the beginning of a string. So, even if there is a matching character after the newline character, It returns none. Let’s look into the below code.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import re
  
  
match = re.search(r'^Geeks', 'Hello,\nGeeks')
print(match)

chevron_right


Output

None

Using the Multiline flag, you can overcome the above issue. It can match against the beginning and end of any line in the string. Let’s match against the beginning of a string.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import re
  
  
match = re.search(r'^Geeks', 'Hello,\nGeeks', re.MULTILINE)
print(match)

chevron_right


Output

<_sre.SRE_Match object; span=(7, 12), match='Geeks'>

Verbose Mode

It allows representing a regular expression in a more readable way. Let’s look at the below code.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


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.

Debug Mode

The re.DEBUG flag provides debugging information while compiling a regular expression. Let’s have a look at the below code.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import re
  
  
match = re.search(r'(?P<first_two>[\d]{2})-(?P<last_three>[\d]{3})',\
                  '25-542', re.DEBUG)
print(match)

chevron_right


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. 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Focused on developing machine learning models and constantly doing research on the complex business challenges to solve problems and deliver valuable insights Expertise includes • Python Programming • Probability and Statistics • Data Modelling and Evaluation • Machine learning algorithms

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.