2011-12-08 132 views
0

我在使用python時遇到了問題。grep前綴python字符串

我有一個文本,例如:

the name of 33e4853h45y45 is one of the 33e445a64b65 and we want all the 33e5c44598e46 to be matched

所以我想找到的文本的字母數字串的所有出現。事情是我知道他們都有「33e」前綴。

現在,我有strings = re.findall(r"(33e+)+", stdout_value)但它不起作用。 我希望能夠回到33e445a64b65, 33e5c44598e46

+0

33e4853h45y45有什麼問題? – Abhijit

回答

2

試試這個

>>> x="the name of 33e4853h45y45 is one of the 33e445a64b65 and we want all the 33e5c44598e46 to be matched" 
>>> re.findall("33e\w+",x) 
['33e4853h45y45', '33e445a64b65', '33e5c44598e46'] 
+0

完美。謝謝! – Duke

+0

如果您認爲有效,您可能想要接受 – Abhijit

+0

將在8分鐘內完成的回答 – Duke

1

這裏有一個微小的變化:

>>> string = '''the name of 33e4853h45y45 is one of the 33e445a64b65 and we want all the 33e5c44598e46 to be matched''' 
>>> re.findall(r"(33e[a-z0-9]+)", string) 
['33e4853h45y45', '33e445a64b65', '33e5c44598e46'] 

相反匹配任何單詞字符,將只匹配數字,小寫數字後, 33e - 這就是[a-z0-9]+的含義。

如果您還想匹配大寫字母,則可以用[a-zA-Z0-9]+替代該部分。