Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this string matches the regular expression?

Tags:

python

regex

Why this string matches the pattern ?

  pattern = """
    ^Page \d of \d$|
    ^Group \d Notes$|
    ^More word lists and tips at http://wwwmajortests.com/word-lists$|
    """
    re.match(pattern, "stackoverflow", re.VERBOSE)

According to me it should match strings like "Page 1 of 1" or "Group 1 Notes".

like image 541
Bunny Rabbit Avatar asked Dec 10 '25 21:12

Bunny Rabbit


1 Answers

In your regular expression, there's trailing |:

# ^More word lists and tips at http://wwwmajortests.com/word-lists$|
#                                                                  ^

Empty pattern matches any string:

>>> import re
>>> re.match('abc|', 'abc')
<_sre.SRE_Match object at 0x7fc63f3ff3d8>
>>> re.match('abc|', 'bbbb')
<_sre.SRE_Match object at 0x7fc63f3ff440>

So, Remove the trailing |.

BTW, you don't need ^ becasue re.match checks for a match only at the beginning of the string.

And, I recommend you to use raw strings(r'....') to correctly escape backslahes.


ADDITIONAL NOTE

\d matches only a single digit. Use \d+ if you also want to match multiple digits.

like image 57
falsetru Avatar answered Dec 13 '25 10:12

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!