这是关于空字符串的不! 已经检查过SO,尤其是question
s1 = '<PERSON>Desi Arnaz</PERSON> was born... intervened on <PERSON>Desi Arnaz</PERSON> behalf. <PERSON>Arnaz</PERSON> and ...'
In python:
>> re.sub("<PERSON>(Desi Arnaz|Desi Arnaz|Arnaz)</PERSON>", "----WORKED------", s1, re.IGNORECASE)
out>>'----WORKED------ was born... intervened on ----WORKED------ behalf. <PERSON>Arnaz</PERSON> and ...'
>> re.findall("<PERSON>(Desi Arnaz|Desi Arnaz|Arnaz)</PERSON>", s1, re.IGNORECASE)
out>>['Desi Arnaz', 'Desi Arnaz', 'Arnaz']
任何人都可以确认这个错误吗?
答案 0 :(得分:1)
这不是一个错误。你搞砸了传递给re.sub
的参数。
re.sub(pattern,repl,string,count = 0,flags = 0)
然而,您将re.IGNORECASE
作为第四个(count
)参数传递,而re.IGNORECASE
恰好是数字2
,因此只会替换前两个匹配项。