1

I'm familiar with how to use something like pattern = re.compile(r"stuff") where stuff is whatever regex pattern I want to use.

What I am trying to figure out is there a way to use the r flag with a variable? Like below

def getFileLike(foo):
   pattern = re.compile(r foo)
   result = re.search(pattern,"some string")

i've tried pattern = re.compile(usr_pattern, -r) and pattern = re.compile(r usr_pattern) but none of it compiles and they throw various errors. So I'm sticking with something like

def getFilesLike(flag):
    if flag:
       pat = re.compile(r".txt")
    else:
       pat = re.compile(r".png")

I've also tried reading the documentation below and also searching for "raw string". I'm not finding anything. I would love it if someone could confirm if this is possible or point me in the right direction.

Lib.py

PyPi regex

1
  • 1
    There is no such thing as a "raw string", there are only raw string literals. You can't use r with a variable because a variable isn't a literal. Commented Jun 14, 2018 at 19:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.