2017-03-02 72 views
1

我是python的新手,並試圖找到執行匹配和替換的最佳和最有效的方法。這是我的問題。推薦Python文本匹配和替換技術

我有一個字典,如下所示的條目。

myDict = {'I HAVE A * NAMED *':'A <star=1> named <star=2> is cool!'} 

我的目標是有一個輸入:

myInput = raw_input() 
# Example input: I HAVE A DOG NAMED MAX 

再搭配與myDict鍵此輸入:

input: 'I HAVE A DOG NAMED *MAX*' matches with dictionary key: 'I HAVE A * NAMED *' 

然後輸出鍵值與明星標籤替換爲缺少的myInput字,DOG和MAX。

output = 'A DOG named MAX is cool!' 

任何聖人的建議非常感謝!

回答

1

這是你想要的嗎?

import re 

myDict = {'I HAVE A (.+) NAMED (.+)':'A <star=1> named <star=2> is cool!'} 
input="I HAVE A dog NAMED max" 

for x in myDict.keys(): 
    if re.match(x,input) : 
     d=myDict[x] 
     for n in range(1, 3): 
      d = d.replace('<star='+str(n)+'>',re.match(x,input).group(n)) 
     print '=>', d 
+0

酷。這工作。我承認我是正則表達式的新手。我會更多地關注它們。謝謝! – kanecain

+0

更好地使用're.sub'。然後,您不必手動進行替換,並且速度更快 – Wombatz

1

這可能是另一種解決方案,你甚至十拿九穩Limberger已經向我們展示了一個很好的解決方案:)

import re 


def convert(sentence, mapping, predefined_regex): 
    for k, v in mapping.iteritems(): 
     p = re.compile(k.format(**predefined_regex)) 
     g = p.search(sentence) 
     if g: 
      keywords = g.groupdict() 
      converted_sentence = v.format(**keywords) 
      return converted_sentence 
    return None 


predefined_regex = { 
    "type": "(?P<type>\w+)", 
    "name": "(?P<name>\w+)" 
} 
my_dict = { 
    "I have [a|an] {type} named {name}": "A {type} named {name} is cool!", 
    "You have [a|an] {type} named {name}": "{name} likes you!" 
} 
sentences = [ 
    "I have a dog named Max.", 
    "You have a cat named Kitty.", 
    "He has a pig named Max" 
] 
for sentence in sentences: 
    converted_sentence = convert(sentence, my_dict, predefined_regex) 
    if not converted_sentence: 
     converted_sentence = "Not found" 
    print("{} -> {}".format(sentence, converted_sentence)) 

參考:

+0

謝謝!我會給它一個鏡頭! – kanecain