Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想使用模式作为原始字符串执行 re.search,如下所示。
m=re.search(r'pattern',string)
但是,如果我在变量中有“模式”,例如pat='pattern'. 如何执行原始搜索?
pat='pattern'
您将模式字符串声明为原始字符串:
regexpattern = r'pattern' m=re.search(regexpattern,string)
您可以通过这种方式提供原始输入。test 是字符串变量。
pat = """pat%s""" % test pattern = re.compile(pat, re.I | re.M) match = pattern.search(l)