我有一个数组,看起来像:
mylist = [
"blah blah hello",
"\nbarnacles and stuff()",
"\nhello again",
"\nother stuff )"
]
我的正则表达式如下:
s = 'hello'
rx = re.compile(s + '.*')
newlist = [ rx.sub(thing, '') for thing in mylist ]
我希望newlist是:
[
"blah blah",
"\nbarnacles and stuff()",
"\n",
"\nother stuff )"
]
相反,我得到了:
[
"",
"",
"",
""
]
怎么回事?这在REPL中表现不一样。。。你知道吗
仔细看the signature for the ^{} method of compiled regexes :
第一个参数是替换字符串,第二个参数是要对其进行操作的字符串,这与您试图调用它的方式相反。(请注意,这与^{} 函数的相对参数顺序相同。)
相关问题 更多 >
编程相关推荐