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

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Brian W
  • 153
  • 1
  • 10
  • 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. – Aran-Fey Jun 14 '18 at 19:34

0 Answers0