Unsolvable Peyon Regex register in 2.6

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 get it to work in Python 2.6, and also in Python 2.7 and 2.6 re.split does not accept the flag (re.IGNORECASE) as the fourth parameter. Any help? Thanks

+5
source share
3 answers

You can simply add (? I) to the regular expression to make case insensitive:

>>> import re
>>> reg = "(foo)(?i)"
>>> re.split(reg, "fOO1foo2FOO3")
['', 'fOO', '1', 'foo', '2', 'FOO', '3']
+12
source

Create re.RegexObjectwith re.compile(), and then call it split().

Example:

>>> re.compile('XYZ', re.IGNORECASE).split('fooxyzbar')
['foo', 'bar']
+3
source

Oh, found it myself, I can compile it into a Regex object:

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

All Articles