dragon dragon - 4 months ago 8
Python Question

extract a string between 2 strings in python?

I need to one give me the string between

~
and
^


i have string like this

:::ABC???,:::DEF???


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>
</td>

Answer

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