Case Insensitive Regex in Python

Case Insensitive Regex in Python

  1. Case Insensitive Regex in Python
  2. Match a String Using the Case Insensitive re.IGNORECASE Flag in Python
  3. Match a String Using the Case Insensitive Marker (?i) in Python
  4. Conclusion

Regular expressions match a particular string within a text in Python. They form a search pattern and check if this search pattern is present in the text or not.

In this article, we will be studying the case insensitive regex in Python. The different ways of performing the case insensitive searches in a text are explained further.

Case Insensitive Regex in Python

Search patterns are made up of a sequence of characters and can be specified using regex rules. However, to work with regular Python expressions, you first need to import the re module.

Case insensitive means that the text should be considered equal in lowercase and uppercase. We need to apply case-insensitive searches in our daily lives very often.

One such example is whenever we search for some commodity, say, a Bag. The information about the Bags will be displayed on the screen.

However, if we search bag in lower case letters or use mixed cases such as bAG, it should also display the same results. Therefore, we need to treat different case letters to be the same to search the results in specific scenarios easily.

Therefore, we use regular expressions which check the case insensitive patterns within a text.

So, let us discuss how to extract a search pattern from a text using regular expressions.

Match a String Using the Case Insensitive re.IGNORECASE Flag in Python

We can use the search(), match(), or sub() functions of Python to find whether our search pattern is present in the text or not and extract their exact positions.

These functions have three parameters:

  • The pattern to be searched.
  • The text in which the pattern is to be searched.
  • A flag.

However, this flag parameter is an optional argument but is used to enable several features in Python.

The re.IGNORECASE is used as a flag to enable case insensitive searching within a text. It will consider the characters [A-Z] the same as [a-z] for a string.

Let us have an example of using the re.IGNORECASE as a flag in our code.

import re
re.search('the', 'ThE', re.IGNORECASE)

Output:

<re.Match object; span=(0, 3), match='ThE'>

Similarly, you can pass the flag as re.IGNORECASE in the match() function or the sub() function to search for a case insensitive string in the text.

However, if you want to search for all the string occurrences in a text, you should use Python’s re.findall() function. It will find all the matched strings that are present in the text.

However, you must pass the flag re.IGNORECASE in the arguments to find the case insensitive strings in a text.

Let us see how to extract all the string occurrences within a text.

import re
re.findall('the', 'The sources informed the police of tHe thieves.', re.IGNORECASE)

Output:

['The', 'the', 'tHe']

The re.IGNORECASE flag, which is used above, can also be written as re.I. This re.I flag is also used to search a case insensitive pattern within a text.

Let us see it with an example.

import re
re.findall('the', 'The sources informed the police of tHe thieves.', re.I)

Output:

['The', 'the', 'tHe']

All these methods are present inside the re module in Python. Therefore, the re module must be imported into the program before using them.

Match a String Using the Case Insensitive Marker (?i) in Python

When you do not want to add the flag parameter in the search() or any other function to extract the string from the text, we use a case insensitive marker denoted by (?i).

It is applied in the regex function before the search pattern without specifying an extra flag parameter.

Below is the code to use the case insensitive marker (?i) with the search() method.

import re
re.search('(?i)TABLE', table)

Output:

<re.Match object; span=(0, 5), match='table'>

However, you can search the pattern within a much larger string and find all the multiple occurrences of the search pattern from the string using the findall() method in Python.

Below is the code snippet to use the case insensitive marker (?i) with the findall() method in Python.

import re
text = "Let it rain, let it snow, let it do!"
re.findall('(?i)LEt' , text)

Output:

['Let', 'let', 'let']

Therefore, the above code snippet outputs all the occurrences of the search pattern within the text. Put the symbol (?i) before the search pattern.

Conclusion

This article has discussed regular expressions and how to use them to find the case-insensitive search patterns within a text. We have used two ways.

First is the re.IGNORECASE flag, which is passed as an argument in the searching functions such as search(), match(), findall(), etc. You can also use the re.I flag to search for the case-insensitive patterns with your string.

However, the second method uses the case insensitive marker (?i), placed before the search pattern in the searching functions.

We can find the case-insensitive patterns in our text using these methods.

Related Article - Python Regex

  • Python Regex Escape
  • Capture Groups With Regular Expression in Python
  • Python - Regular Expression to Match a Multiline Block of Text
  • Check if String Matches Regex in Python
  • Python Regex Group