0

I have this, which gives no match, why ?:

>>> p = r'abc'
>>> cp = re.compile(p, re.IGNORECASE)
>>> m = cp.search('ABC', re.IGNORECASE)
>>> m # NO MATCH # 1

But, this gives a match, why ?

>>> m = cp.search('  ABC', re.IGNORECASE) # introduced spaces, ABC no longer at the beginning 
>>> m # MATCH # 2
<_sre.SRE_Match object at 0x1082b5ac0>

To get a match in #1 all I have to do is drop re.IGNORECASE, why so ?

>>> m = cp.search('ABC')
>>> m
<_sre.SRE_Match object at 0x10827e308>

1 Answer 1

1

If (in the interpreter) you print out the help on cp.search:

help(cp.search)

you will see:

Help on built-in function search:
search(string=None, pos=0, endpos=9223372036854775807, *, pattern=None) method of _sre.SRE_Pattern instance
    Scan through string looking for a match, and return a corresponding match object instance.

    Return None if no position in the string matches.

Notice that this function does not take any flags, but what you were actually passing your flag to the 'pos' argument, which was why you got an unexpected result.

Sign up to request clarification or add additional context in comments.

4 Comments

How'd you get pos=0, endpos=9223372036854775807 in your method help ? I don't see that. But what you said makes total sense. Thanks
@abc No idea, maybe we're running different versions of Python.
$ python --version Python 2.7.10
@abc I was using Python 3.6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.