AlexC
AlexC

Reputation: 3904

Why does this string not match the pattern

I expected the word '789' to match my pattern '\b[7][8-9]{2}\b' in the following example:

def test_4(self):
    text = 'asdf 2345 gfdf 44 dfg 79878 dsfg 78998 sdfg 789 7989 '
    pattern = '\b[7][8-9]{2}\b'
    match = re.search(pattern, text)
    if match:
        print(pattern)
        print(match.group())

Why does the text not match the pattern?

Upvotes: 0

Views: 2234

Answers (1)

Blckknght
Blckknght

Reputation: 104732

The \bs you have in your pattern string are being interpreted by Python as the ASCII backspace character ('\x08') before the regex engine can see them and interpret them as a word break. In order to stop the first interpretation, use a "raw" string by prefixing the opening quotation mark with r (you could instead escape the backslash with an extra one, but that's usually uglier and less easy to understand than a raw string):

r'\b[7][8-9]{2}\b'

Note that the brackets around the 7 and the dash in the [8-9] are unnecessary (you could just use r'\b7[89]{2}\b').

Upvotes: 2

Related Questions