You can use the re.split function with the re.IGNORECASE flag (or re.I for short):
>>> import re
>>> test = "hI MY NAME iS FoO bar"
>>> re.split("foo", test, flags=re.IGNORECASE)
['hI MY NAME iS ', ' bar']
>>>
Answer from user2555451 on Stack OverflowYou can use the re.split function with the re.IGNORECASE flag (or re.I for short):
>>> import re
>>> test = "hI MY NAME iS FoO bar"
>>> re.split("foo", test, flags=re.IGNORECASE)
['hI MY NAME iS ', ' bar']
>>>
This is not the exact answer but the solution based on the question. After searching awhile on the net I implemented the following.
This is my custom tag (see how to do it).
from django.template.defaultfilters import register
from django.utils.html import escape
from django.utils.safestring import mark_safe
@register.simple_tag
def highlight(string, entry, prefix, suffix):
string = escape(string)
entry = escape(entry)
string_upper = string.upper()
entry_upper = entry.upper()
result = ''
length = len(entry)
start = 0
pos = string_upper.find(entry_upper, start)
while pos >= 0:
result += string[start:pos]
start = pos + length
result += prefix + string[pos:start] + suffix
pos = string_upper.find(entry_upper, start)
result += string[start:len(string)]
return mark_safe(result)
It accepts unsafe string and returns the escaped result.
Use it this way:
<span class="entityCode">{% highlight entity.code search_text '<span class="highlighted">' '</span>' %}</span>
I use a nested <span> to inherit all the style. And it shows something like

Python regex split case insensitive in 2.6 - Stack Overflow
Case insensitivity in Python strings - Stack Overflow
Python
1 -Use ‘for’, ‘.split()’, and ‘if’ to print out words
that start with ‘b’(case-sensitive).
Input:
'You cannot end a
sentence because because because is a conjunction.'
Output:
because
because
python - Case insensitive replace - Stack Overflow
Use a regex instead:
>>> import re
>>> regex = re.compile(r"\s*feat\.\s*", flags=re.I)
>>> regex.split("abc feat. def")
['abc', 'def']
>>> regex.split("abc Feat. def")
['abc', 'def']
or, if you don't want to allow FEAT. or fEAT. (which this regex would):
>>> regex = re.compile(r"\s*[Ff]eat\.\s*")
a[0:a.lower().find("feat.")].rstrip() would do.
anding
"string1" and "string2" and ... and "stringN"
returns the the last string.
oring
"string1" or "string2" or ... or "stringN"
would return the first string.
Short-circuit evaluation.
You can just add (?i) to the regular expression to make it case insensitive:
>>> import re
>>> reg = "(foo)(?i)"
>>> re.split(reg, "fOO1foo2FOO3")
['', 'fOO', '1', 'foo', '2', 'FOO', '3']
Create a re.RegexObject using re.compile() and then call it's split() method.
Example:
>>> re.compile('XYZ', re.IGNORECASE).split('fooxyzbar')
['foo', 'bar']
You can supply the flag re.IGNORECASE to functions in the re module as described in the docs.
matcher = re.compile(myExpression, re.IGNORECASE)
Using re is the best solution even if you think it's complicated.
To replace all occurrences of 'abc', 'ABC', 'Abc', etc., with 'Python', say:
re.sub(r'(?i)abc', 'Python', a)
Example session:
>>> a = 'abc asd Abc asd ABCDE XXAbCXX'
>>> import re
>>> re.sub(r'(?i)abc', 'Python', a)
'Python asd Python asd PythonDE XXPythonXX'
>>>
Note how embedding (?i) at the start of the regexp makes it case insensitive. Also note the r'...' string literal for the regexp (which in this specific case is redundant but helps as soon as you use a regexp that has backslashes (\) in them.
The string type doesn't support this. You're probably best off using the regular expression sub method with the re.IGNORECASE option.
>>> import re
>>> insensitive_hippo = re.compile(re.escape('hippo'), re.IGNORECASE)
>>> insensitive_hippo.sub('giraffe', 'I want a hIPpo for my birthday')
'I want a giraffe for my birthday'
import re
pattern = re.compile("hello", re.IGNORECASE)
pattern.sub("bye", "hello HeLLo HELLO")
# 'bye bye bye'
How do I count how many times a case sensitive word appears somewhere using a boolean.
So for example:
word = input("Search this word: ")Then I want to check if case sensitive == True or False. If it's true, it will only find the exact same words.
Also, the word can't be a part of another word. So it may not find "Apple" in Applepie. But it can only find "Apple" if it's independent like this: Apple Pie