2010-04-30 42 views
16

我有Python匹配並返回之間的字符串

stringA = "xxxxxxFoundAaaaaaaaaaaaaaaFoundBxxxxxxx" 
stringB = "FoundA" 
stringC = "FoundB" 

如何在python中執行正則表達式以返回aaaaaaaaaaaaa?

請幫忙。 先謝謝。

+0

如果你有'stringA = 「xxxxxxFoundAaaaaaFoundAaaaaFoundBaaaaaFoundBxxxxxxx」'會發生什麼??? – nosklo 2010-04-30 17:50:58

回答

15
>>> 
>>> stringA = "xxxxxxFoundAaaaaaaaaaaaaaaFoundBxxxxxxx" 
>>> stringB = "FoundA" 
>>> stringC = "FoundB" 
>>> 
>>> import re 
>>> re.search(re.escape(stringB)+"(.*?)"+re.escape(stringC),stringA).group(1) 
'aaaaaaaaaaaaaa' 
>>> 
+1

**失敗**沒有使用re.escape()** – 2010-04-30 05:14:30

+0

我添加了@John。 – YOU 2010-04-30 05:19:45

4
re.search(re.escape(stringB) + "(.*?)" + re.escape(stringC), stringA).group(1) 
相關問題