Word Separation (not case sensitive)

If i want to take

"hi, my name is foo bar"

and divide it into "foo", and share it to register a case-insensitive (to split on any of the "foo", "foo", "foo", and so on), what do I do? Keep in mind that although I would like split to be case insensitive, I also want to keep case insensitive of the rest of the string.

So, if I have:

test = "hi, my name is foo bar"

print test.split('foo')

print test.upper().split("FOO")

I would get

['hi, my name is ', ' bar']
['HI, MY NAME IS ', ' BAR']

respectively.

But what I want :

['hi, my name is ', ' bar']

everytime. The goal is to maintain case sensitivity of the original string, except that I am sharing.

So, if my test line is:

"hI MY NAME iS FoO bar"

my desired result:

['hI MY NAME iS ', ' bar']
+4
source share
2 answers

re.split re.IGNORECASE ( re.I ):

>>> import re
>>> test = "hI MY NAME iS FoO bar"
>>> re.split("foo", test, flags=re.IGNORECASE)
['hI MY NAME iS ', ' bar']
>>>
+10

- . "". ( #, , )

-1

All Articles