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.