dragon dragon - 1 year ago 42
Python Question

extract a string between 2 strings in python?

I need to one give me the string between


i have string like this


I need to get the string between them with python

I want to do all this because i am trying to extract text from an html page. like this example

<td class="cell-1">
<div><span class="value-frame">&nbsp;~ABC^,~DEF^</span></div>

Answer Source

It seems like you want ABC and DEF , so you need write re like this (.*?)

import re
target = ' <td class="cell-1"><div><span class="value-frame">&nbsp;~ABC^,~DEF^</span></div></td>'
matchObj = re.findall(r'~(.*?)\^', target)
print matchObj 
# ['ABC', 'DEF']

you can learn more about re module