正则表达式来自变量的字符串,而不是字符串文字

时间:2014-05-06 03:45:35

标签: python

import re

keyword = 'jesus'

re.search(r'\D*keyword', 'jesuscrhist')

我如何以'jesus'编译而不是'keyword'的方式将变量放在那里?

换句话说,我该如何组装:

keyword = "foobar"
some_string = "What's the difference between rebar and keyword?"

这样

assert(some_string == "What's the difference between rebar and fubar?")

0 个答案:

没有答案