Pass re.IGNORECASE to the flags param of search, match, or sub:

re.search('test', 'TeSt', re.IGNORECASE)
re.match('test', 'TeSt', re.IGNORECASE)
re.sub('test', 'xxxx', 'Testing', flags=re.IGNORECASE)
Answer from Michael Haren on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-case-insensitive-string-replacement
Case insensitive string replacement in Python - GeeksforGeeks
July 23, 2025 - Explanation: re.sub(r"(?i)best", lambda m: "good", a) uses a case-insensitive regex ((?i) inline flag) to match the word "best" in any casing within the string a, and replaces each match using a lambda function that returns "good".
🌐
TutorialsPoint
tutorialspoint.com β€Ί article β€Ί how-to-write-a-case-insensitive-python-regular-expression-without-re-compile
How to write a case insensitive Python regular expression without re.compile?
March 24, 2026 - import re text = "Python is GREAT for data science. Great choice!" new_text = re.sub(r"great", "fantastic", text, flags=re.IGNORECASE) print(new_text) Python is fantastic for data science. fantastic choice! Both "GREAT" and "Great" are replaced with "fantastic" because of the case-insensitive flag.
Discussions

python - Case insensitive regular expression without re.compile? - Stack Overflow
In Python, I can compile a regular expression to be case-insensitive using re.compile: More on stackoverflow.com
🌐 stackoverflow.com
python - Case insensitive replace - Stack Overflow
Possibly the fastest method for a case-insensitive replace, tested against both using an arrayed string and using regex. 2021-07-01T00:56:24.887Z+00:00 ... Like Blair Conrad says string.replace doesn't support this. Use the regex re.sub, but remember to escape the replacement string first. More on stackoverflow.com
🌐 stackoverflow.com
python - Issue with case-insensitive regex pattern for re.sub() - Stack Overflow
import re from opswareConnect import ... = re.sub('[.1DC.com]\\b', '', str(arg1)) print arg1 ... But [.1DC.com] is a character group... ... The regular expression is \.1dc\.com. The backslash escapes the dot which normally matches any character rather than just a period. Make the search case insensitive with the ... More on stackoverflow.com
🌐 stackoverflow.com
python - Why doesn't ignorecase flag (re.I) work in re.sub() - Stack Overflow
From pydoc: re.sub = sub(pattern, repl, string, count=0, flags=0) Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the More on stackoverflow.com
🌐 stackoverflow.com
🌐
AskPython
askpython.com β€Ί python-modules β€Ί python-regex-for-case-insensitive-text-matching
Python Regex for Case-Insensitive Text Matching without re.compile() - AskPython
May 12, 2023 - re.sub()-> This function replaces the matched string of the expression and replaces it with something else. Related: Matching Entire Strings in Python using Regular Expressions.
🌐
w3resource
w3resource.com β€Ί python-exercises β€Ί re β€Ί python-re-exercise-44.php
Python: Do a case insensitive string replacement - w3resource
Write a Python program to do case-insensitive string replacement. ... import re text = "PHP Exercises" print("Original Text: ",text) redata = re.compile(re.escape('php'), re.IGNORECASE) new_text = redata.sub('php', 'PHP Exercises') print("Using 'php' replace PHP") print("New Text: ",new_text)
🌐
Mycoding
mycoding.uk β€Ί a β€Ί python__how_to_do_case_insensitive_string_replacement.html
Python: How to do Case insensitive string replacement
Also, we will pre-compile search patter into regular expression for future use with ignoring case Β· import re # import module for regular expressions test_string = 'One two oNe Two onE tWo ONE TWO one tWO' # string to be replaces pattern = 'two' # pattern for seach replace = '222' # patter for replacement #replace = 'TWO' compiled = re.compile(re.escape(pattern), re.IGNORECASE) # pre-compiled pattern new_string = compiled.sub(replace, test_string) # replace with pre-compiled pattern print( new_string) # One 222 oNe 222 onE 222 ONE 222 one 222
Find elsewhere
🌐
Real Python
realpython.com β€Ί lessons β€Ί replace-string-python-case-insensitive
Be Case-Insensitive (Video) – Real Python
Be Case-Insensitive 02:08 Β· Look for a Character Set 01:55 Β· Append a Quantifier 02:18 Β· Recap Your Regex Patterns 00:13 Β· Working With Regular Expressions Β· Get to Know re.sub() 01:09 Β· Use re.sub() 05:11 Β· Replace a String in Python (Summary) 02:08 Β·
Published: August 22, 2023
🌐
LearnByExample
learnbyexample.github.io β€Ί py_regular_expressions β€Ί flags.html
Flags - Understanding Python re(gex)?
1) Remove from the first occurrence of hat to the last occurrence of it for the given input strings. Match these markers case insensitively. >>> s1 = 'But Cool THAT\nsee What okay\nwow quite' >>> s2 = 'it this hat is sliced HIT.' >>> pat = re.compile() ##### add your solution here >>> pat.sub('', s1) 'But Cool Te' >>> pat.sub('', s2) 'it this .'
🌐
ProgramCreek
programcreek.com β€Ί python β€Ί example β€Ί 96 β€Ί re.IGNORECASE
Python Examples of re.IGNORECASE
def normalize_release_tags(name, real_title): import re import xbmc def _normalize_name(val): proper = re.sub(r"[\[\(\]\)]", "", val) proper = re.sub(r"'", "", proper) proper = re.sub(r"\W", " ", proper) proper = re.sub(r"\s+", " ", proper) return proper name = _normalize_name(name) real_title = _normalize_name(real_title) release_tags = re.compile(real_title, re.IGNORECASE).sub("", name) tags = map(re.escape, VIDEO_CODECS.keys() + AUDIO_CODECS.keys() + RESOLUTIONS.keys()) release_tags = re.compile("(%s)" % "|".join(tags), re.IGNORECASE).sub("", release_tags) release_tags = re.sub(r"\s+", " ", release_tags) return release_tags.strip()
🌐
Real Python
realpython.com β€Ί lessons β€Ί replace-string-python-resub
Use re.sub() (Video) – Real Python
The re.IGNORECASE flag makes it a case-insensitive pattern. So now any substring containing "blast", regardless of capitalization, will be matched and replaced. And finally, you print the transcript again.
Published: August 22, 2023
🌐
Finxter
blog.finxter.com β€Ί 5-best-ways-to-perform-case-insensitive-string-replacement-in-python
5 Best Ways to Perform Case Insensitive String Replacement in Python – Be on the Right Side of Change
February 27, 2024 - This code snippet first imports the re module. It then defines a pattern to look for in the text, a replacement string, and uses the re.sub() function, passing the re.IGNORECASE flag to replace all case insensitive occurrences of the pattern with the replacement string.
🌐
Index.dev
index.dev β€Ί blog β€Ί regex-advanced-string-replacement-python
Python Regex Replace: How to Replace Strings Using re Module
By default, regex in Python is case-sensitive. If you want to perform case-insensitive replacements, you can use the re.IGNORECASE flag. Additionally, regex can work across multiple lines if the string contains line breaks.
🌐
TutorialsPoint
tutorialspoint.com β€Ί article β€Ί case-insensitive-string-replacement-using-python-program
Case-insensitive string replacement using Python Program
March 26, 2026 - You can use the (?i) inline flag for case-insensitive matching without compiling ? import re input_string = "Hello TutorialsPOINT Python" substring = "tutorialspoint" replace_string = "Java" # use (?i) for case-insensitive matching result = re.sub('(?i)' + re.escape(substring), replace_string, input_string) print("Result:", result)
🌐
Python Module of the Week
pymotw.com β€Ί 2 β€Ί re
re – Regular Expressions - Python Module of the Week
Pattern : \bT\w+ Case-sensitive : ['This'] Case-insensitive: ['This', 'text'] There are two flags that effect how searching in multi-line input works. The MULTILINE flag controls how the pattern matching code processes anchoring instructions for text containing newline characters. When multiline mode is turned on, the anchor rules for ^ and $ apply at the beginning and end of each line, in addition to the entire string. import re text = 'This is some text -- with punctuation.\nAnd a second line.' pattern = r'(^\w+)|(\w+\S*$)' single_line = re.compile(pattern) multiline = re.compile(pattern, re.MULTILINE) print 'Text :', repr(text) print 'Pattern :', pattern print 'Single Line :', single_line.findall(text) print 'Multline :', multiline.findall(text)
🌐
Delft Stack
delftstack.com β€Ί home β€Ί howto β€Ί python β€Ί python regex case insensitive
Case Insensitive Regex in Python | Delft Stack
December 6, 2023 - So, let us discuss how to extract a search pattern from a text using regular expressions. When you want to match a string in Python using a case-insensitive approach, you can leverage the re.IGNORECASE flag or the re.I method within the re.findall ...