Python Flags to Tune the Behavior of Regular Expressions
Last Updated :
01 Oct, 2020
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
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
import re
match = re.search(r 'geeksforgeeks' , 'GeeksforGeeks' ,re.IGNORECASE)
print (match)
|
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
import re
match = re.search(r '.+' , 'Hello,\nGeeks' )
print (match)
|
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
import re
match = re.search(r '.+' , 'Hello,\nGeeks' , re.DOTALL)
print (match)
|
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
import re
match = re.search(r '^Geeks' , 'Hello,\nGeeks' )
print (match)
|
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
import re
match = re.search(r '^Geeks' , 'Hello,\nGeeks' , re.MULTILINE)
print (match)
|
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
import re
match = re.search(r
, '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.
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.
Similar Reads
Extracting email addresses using regular expressions in Python
Let suppose a situation in which you have to read some specific data like phone numbers, email addresses, dates, a collection of words etc. How can you do this in a very efficient manner?The Best way to do this by Regular Expression. Let take an example in which we have to find out only email from t
3 min read
Regular Expression (RegEx) in Python with Examples
A Regular Expression or RegEx is a special sequence of characters that uses a search pattern to find a string or set of strings. It can detect the presence or absence of a text by matching it with a particular pattern and also can split a pattern into one or more sub-patterns. Regex Module in Python
15+ min read
Regular Expressions in Python - Set 2 (Search, Match and Find All)
Regular Expression in Python with Examples | Set 1The module re provides support for regular expressions in Python. Below are main methods in this module. Searching an occurrence of pattern re.search() : This method either returns None (if the pattern doesn't match), or a re.MatchObject that contain
4 min read
How Can I Find All Matches to a Regular Expression in Python?
In Python, regular expressions (regex) are a powerful tool for finding patterns in text. Whether we're searching through logs, extracting specific data from a document, or performing complex string manipulations, Python's re module makes working with regular expressions straightforward. In this arti
3 min read
Parsing and Processing URL using Python - Regex
Prerequisite: Regular Expression in Python URL or Uniform Resource Locator consists of many information parts, such as the domain name, path, port number etc. Any URL can be processed and parsed using Regular Expression. So for using Regular Expression we have to use re library in Python. Example: U
3 min read
How to Import Regex in Python
Regex is a built-in library in Python. You can use the re module to work with regular expressions. Here are some basic examples of how to use the re module in Python: Examples of Regular Expression in PythonWe can import it in our Python by writing the following import statement in the Python script
2 min read
Categorize Password as Strong or Weak using Regex in Python
Given a password, we have to categorize it as a strong or weak one. There are some checks that need to be met to be a strong password. For a weak password, we need to return the reason for it to be weak. Conditions to be fulfilled are: Minimum 9 characters and maximum 20 characters.Cannot be a newli
2 min read
Python - Replace all occurrences of a substring in a string
Replacing all occurrences of a substring in a string means identifying every instance of a specific sequence of characters within a string and substituting it with another sequence of characters. Using replace()replace () method is the most straightforward and efficient way to replace all occurrence
2 min read
How to search and replace text in a file in Python ?
In this article, we will learn how we can replace text in a file using python. Method 1: Searching and replacing text without using any external module Let see how we can search and replace text in a text file. First, we create a text file in which we want to search and replace text. Let this file b
5 min read
How to remove text inside brackets in Python?
In this article, we will learn how to remove content inside brackets without removing brackets in python. Examples: Input: (hai)geeks Output: ()geeks Input: (geeks)for(geeks) Output: ()for() We can remove content inside brackets without removing brackets in 2 methods, one of them is to use the inbui
4 min read