2010-04-30 10 views
16

mamPython mecz i powrót ciąg pomiędzy

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

Jak zrobić wyrażenia regularnego w Pythonie w celu powrotu aaaaaaaaaaaaaa?

Proszę o pomoc. Z góry dzięki.

+0

, co się dzieje, jeśli masz 'Stringa = "xxxxxxFoundAaaaaaFoundAaaaaFoundBaaaaaFoundBxxxxxxx"' ??? – nosklo

Odpowiedz

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

** FAIL ** nie użył re .escape() ** –

+0

Dodałem @John. – YOU

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