fedorqui
fedorqui

Reputation: 289985

Compile a regex with one pattern per line

I have multiple patterns to check against. Say hello and bye, but will be many more, so I chose to use re.compile() to store them and then being able to check this regex:

import re

mypatt = re.compile(r'(hello|bye)', re.IGNORECASE)
url = ["bye bye", "hello how are you", "i am fine", "ok byeee"]
for u in url:
    if mypatt.search(u):
        print "yes --> %s" %(u)

Upon running this code I get the desired output:

yes --> bye bye
yes --> hello how are you
yes --> ok byeee

However, since there are multiple patterns I would like to write one per line, with something like:

mypatt = re.compile(r'(\
    hello|\
    bye\
    )', re.IGNORECASE)

However this does not work and I cannot understand why. What is the way to write such statement, writing every pattern in a different line?

Upvotes: 1

Views: 133

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122091

You're creating a pattern that includes the whitespace at the start of each line. To avoid this, either:

  1. Use textwrap.dedent, which removes common leading whitespace from each line in a multiline string; or

  2. Add the re.VERBOSE (or re.X) flag to ignore unescaped whitespace and allow the addition of inline comments.

Upvotes: 3

Related Questions