Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python match and return string in between

Tags:

python

regex

I have following code

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

How do I do a regular expression in python in order to return aaaaaaaaaaaaaa?

like image 337
Peter S. Avatar asked Apr 30 '10 04:04

Peter S.


2 Answers

>>>
>>> stringA = "xxxxxxFoundAaaaaaaaaaaaaaaFoundBxxxxxxx"
>>> stringB = "FoundA"
>>> stringC = "FoundB"
>>>
>>> import re
>>> re.search(re.escape(stringB)+"(.*?)"+re.escape(stringC),stringA).group(1)
'aaaaaaaaaaaaaa'
>>>
like image 193
YOU Avatar answered Oct 12 '22 04:10

YOU


re.search(re.escape(stringB) + "(.*?)" + re.escape(stringC), stringA).group(1)
like image 21
John Machin Avatar answered Oct 12 '22 04:10

John Machin