1
votes

I have the following regex in python to parse listings in text:

re.compile('(.*,?) and (.*)')

Everything works as expected, except when the re is compiled with the re.IGNORECASE flag, then the first two chars are not returned.

Output from re group 1 when compiled without the IGNORECASE flag:

the 1689 London Baptist Confession of Faith , 1742 Philadelphia Baptist Confession , the 1833 New Hampshire Baptist Confession of Faith , the Southern Baptist Convention 's Baptist Faith and Message ,

Output from re group 1 when compiled with the IGNORECASE flag:

e 1689 London Baptist Confession of Faith , 1742 Philadelphia Baptist Confession , the 1833 New Hampshire Baptist Confession of Faith , the Southern Baptist Convention 's Baptist Faith and Message ,

The documentation regarding the flag states:

Perform case-insensitive matching; expressions like [A-Z] will match lowercase letters, too. This is not affected by the current locale. To get this effect on non-ASCII Unicode characters such as ü and Ü, add the UNICODE flag.

So no hint regarding this behaviour, what could explain it or am I missing something obvious here? EDIT: As requested in the comments, the full code example (Python 3.6.5):

listing_re = re.compile('(.*,?) and (.*)')
def parse_listing(txt):
    listing_search = listing_re.search(txt, re.IGNORECASE)
    if listing_search:
        seperated_by_comma = listing_search.group(1)    # listing of expressions, seperated by ','
        parts = seperated_by_comma.split(',')           # split string at ','
        parts.append(listing_search.group(2))           # append the single expression after 'and'
        return [x.strip() for x in parts if x.strip()]  # return list of stripped exressions
    return list()

print(parse_listing("the 1689 London Baptist Confession of Faith , 1742 Philadelphia Baptist Confession , the 1833 New Hampshire Baptist Confession of Faith , the Southern Baptist Convention 's Baptist Faith and Message , and written church covenants"))

Code above returns wrong result, removing the re.IGNORECASE flag returns the correct result.

1
Can't reproduce on Python 3.4 - 3.7. Please provide full code that shows this. - DeepSpace

1 Answers

5
votes

The problem here is that you're passing the re.IGNORECASE flag in the wrong place. Since listing_re is a compiled regex, listing_re.search has a signature like this (docs):

Pattern.search(string[, pos[, endpos]])

[...]

The optional second parameter pos gives an index in the string where the search is to start; it defaults to 0. This is not completely equivalent to slicing the string; the '^' pattern character matches at the real beginning of the string and at positions just after a newline, but not necessarily at the index where the search is to start.

As you can see, you've passed re.IGNORECASE as the value of the pos parameter. Since re.IGNORECASE happens to have a value of 2, you end up skipping the first 2 characters.

>>> re.IGNORECASE
<RegexFlag.IGNORECASE: 2>

The correct usage would be to pass the flags to re.compile:

listing_re = re.compile('(.*,?) and (.*)', re.IGNORECASE)