Python找到所有匹配的子串模式并替换substring

时间:2013-06-19 02:21:32

标签: python string replace

我想搜索句子是否有特定模式。如果没找到,什么都不做。如果找到模式,则将string替换为字符串中的另一个子字符串。

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 '' ?

到目前为止,我设法找到了句子中的第一个匹配而不是下一个。如何搜索匹配模式的所有事件?

1 个答案:

答案 0 :(得分:4)

使用正则表达式:

import re

def findSubString(raw_string, start_marker, end_marker):
    return re.sub(
        r'(?<={}).*?(?={})'.format(re.escape(start_marker), re.escape(end_marker)),
        lambda m: m.group().strip().replace(' ', '_'),
        raw_string)

line1 = "Who acted as `` Bruce Wayne '' in the movie `` Batman Forever '' ?"
line1 = findSubString(line1, "``", "''")
assert line1 == "Who acted as ``Bruce_Wayne'' in the movie ``Batman_Forever'' ?"

没有正则表达式:

def findSubString(raw_string, start_marker, end_marker): 
    result = []
    rest = raw_string
    while True:
        head, sep, tail = rest.partition(start_marker)
        if not sep:
            break
        body, sep, tail = tail.partition(end_marker)
        if not sep:
            break
        result.append(head + start_marker + body.strip().replace(' ', '_') + end_marker)
        rest = tail
    result.append(rest)
    return ''.join(result)