我想搜索一个句子是否有特定的模式。如果没有找到,什么也不做。如果找到模式,则用字符串中的另一个子字符串替换模式。
line1 = "Who acted as `` Bruce Wayne '' in the movie `` Batman Forever '' ?"
#Desired Result: Who acted as ``Bruce_Wayne'' in the movie ``Batman_Forever'' ?
#This is what I have tried..
def findSubString(raw_string, start_marker, end_marker):
start = raw_string.index(start_marker) + len(start_marker)
end = raw_string.index(end_marker, start)
return raw_string[start:end]
phrase = findSubString(line1, "``", "''")
newPhrase = phrase.strip(' ').replace(' ', '_')
line1 = line1.replace(phrase, newPhrase)
当前结果:Who acted as ``Bruce_Wayne'' in the movie `` Batman Forever '' ?
到目前为止,我设法找到了句子中的第一个出现,但没有找到下一个。如何搜索所有匹配模式的匹配项?