python - Split by a word (case insensitive) -
if want take
"hi, name foo bar"
and split on "foo"
, , have split case insensitive (split on of "foo"
, "foo"
, "foo"
, etc), should do? keep in mind although have split case insensitive, want maintain case sensitivity of rest of string.
so if have:
test = "hi, name foo bar" print test.split('foo') print test.upper().split("foo")
i
['hi, name ', ' bar'] ['hi, name ', ' bar']
respectively.
but want is:
['hi, name ', ' bar']
every time. goal maintain case sensitivity of original string, except splitting on.
so if test string was:
"hi name foo bar"
my desired result be:
['hi name ', ' bar']
you can use re.split
function re.ignorecase
flag (or re.i
short):
>>> import re >>> test = "hi name foo bar" >>> re.split("foo", test, flags=re.ignorecase) ['hi name ', ' bar'] >>>
Comments
Post a Comment