Sunday, March 27, 2011

Case insensitive Python regular expression without re.compile

In Python, I can compile a regular expression to be case-insensitive using re.compile:

>>> s = 'TeSt'
>>> casesensitive = re.compile('test')
>>> ignorecase = re.compile('test', re.IGNORECASE)
>>> 
>>> print casesensitive.match(s)
None
>>> print ignorecase.match(s)
<_sre.SRE_Match object at 0x02F0B608>

Is there a way to do the same, but without using re.compile. I can't find anything like perl's i suffic (e.g. m/test/i) in the documentation.


This is somewhat related to my previous question Is it worth using Python’s re.compile? but different enough to ask separately.

From stackoverflow
  • It looks like you just need to pass it as a param to search or match:

    re.search('test', 'TeSt', re.IGNORECASE)
    re.match('test', 'TeSt', re.IGNORECASE)
    

    For re.sub, it looks like this might help:

    result = re.sub(r'(?i)test’, ’XXXX′, 'TeSt')
    
    Mat : Cool. That works for search and match. I was actually looking for doing it with re.sub when I was searching around, but simplified my example posted here too much! re.sub('test', 'xxxx', 'Testing', re.IGNORECASE) doesn't work.
    Michael Haren : I updated my answer with something else that might work
  • I'm not overly familiar with Python, but this is one way to do it in Perl which might be portable to Python:

    #!/usr/bin/perl
    $x = "Test";
    (lc $x) ~= /test/;  # this effectively makes the regex case 
                        # insensitive, assuming that the regex is all 
                        # lower case.
    (uc $x) ~= /TEST/;  # this does the same if you happen to prefer 
                        # upper case.
    

0 comments:

Post a Comment