Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex split case insensitive in 2.6

Tags:

python

regex

I have the following code that works in Python 2.7:

entry_regex = '(' + search_string + ')'
entry_split = re.split(entry_regex, row, 1, re.IGNORECASE)

I need to make it work in Python 2.6 as well as in Python 2.7 and 2.6 re.split doesn't accept a flag (re.IGNORECASE) as forth parameter. Any help? Thanks

like image 870
pistacchio Avatar asked Jan 24 '12 20:01

pistacchio


1 Answers

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']
like image 75
Bill Gribble Avatar answered Oct 26 '22 05:10

Bill Gribble