2013-06-19 56 views
2

我想搜索一個句子是否具有特定模式。如果找不到,什麼也不做。如果發現模式,則將該模式替換爲字符串中的另一個子字符串。Python找到所有匹配的子字符串模式並替換子字符串

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

到目前爲止,我設法找到在句子中第一次出現,但不是未來。如何搜索匹配模式的所有匹配項?

+0

,你可以在你的字符串換行符,和你的標記之間?這對一些可能的解決方案有影響,因爲換行符是字符串的自然「結束」,可以用特殊的方式處理(例如通過're'模塊)。 – EOL

+0

@EOL - 標記之間的字符不在同一句中。沒有換行符。 – Cryssie

回答

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) 
+0

用於正則表達式部分的+1:非常好的使用lookbehead,lookahead和非貪婪匹配以及逃脫! – EOL

相關問題