
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Case-Insensitive Python Regular Expression Without Re-Compile
By default, Python regular expressions (regex) look for case-sensitive matches. That is, the terms "apple" and "Apple" are not synonyms. But sometimes we want to search without consideration for uppercase or lowercase.
This is referred to as a case-insensitive match. This chapter teaches you how to create case-insensitive regexes in Python without using re.compile().
What is a Regular Expression?
A regular expression (regex) is a pattern that enables you to find strings like -
Emails
Phone Numbers
Names
Or any pattern.
To interact with regular expressions, we use the Python built-in module re.
Import the re Module
Before you use regex make sure you always import the re module -
import re
How to Make Regex Case-Insensitive Without re.compile
You can make your regex case-insensitive by providing the flags=re.IGNORECASE argument ?
re.function(pattern, string, flags=re.IGNORECASE)
Here you can replace the function with -
search
match
findall
sub, etc.
Using Case Insensitive Regular Expressions
In the following example, let us assume 'Python is a versatile programming language.' as a string. Here we have to find the occurrences of the word 'python' regardless of its case.
Example
The following Python code shows how to perform a case-insensitive search in a string using regular expressions. We can set the case-insensitive flag directly in the function call, which removes the need to create the regex pattern.
import re text = "Python is a versatile programming language." matches = re.findall(r"python", text, re.IGNORECASE) print(matches)
In this example, we utilize the re.IGNORECASE flag within the call to re.findall() to ensure that the search is case insensitive.
On executing the above code snippet, the below output is obtained.
['Python']
Using re.search() for Case Insensitivity
Take this example: 'Learning Python can be fun.' We want to find the occurrence of the word 'learning' in a case-insensitive manner.
Example
Here, we will use the re.search() function instead, which searches for the pattern in the string. The case insensitivity flag will again be included directly in the function call.
import re text = "Learning Python can be fun." match = re.search(r"learning", text, re.IGNORECASE) if match: print(f"Found: {match.group()}") else: print("Not found")
The re.search() function returns a match object if the pattern is found; otherwise, it returns None.
On executing the above code snippet, the below output is obtained.
Found: Learning
Using re.sub() for Case Insensitive Replacements
In our next example, consider 'Python is great for data science.'. We will replace the word 'great' with 'fantastic' in a case-insensitive manner.
Example
We will make use of the re.sub() function, which allows us to replace patterns found in strings. The case insensitivity flag will be applied directly within the function call.
import re text = "Python is great for data science." new_text = re.sub(r"great", "fantastic", text, flags=re.IGNORECASE) print(new_text)
The re.sub() function returns a new string with all occurrences of the pattern replaced by the specified replacement string.
On executing the above code snippet, the following output is obtained.
Python is fantastic for data science.