提取使用python 2 chracters之间的串 [英] Extracting a string between 2 chracters using python
本文介绍了提取使用python 2 chracters之间的串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个Python的正则表达式来给我所有〜
之间的字符串和 ^
从这样的字符串:
~~~~ ^ AAA BBB CCC ^> DDD ^
我试过这样:
进口重
目标='~~~~ AAA> ^ ^ BBB CCC> DDD ^
matchObj =通过re.findall(R'〜(*?)\\ ^',目标)
打印matchObj
但结果是:
['~~~ ABC']
我想到的是:
['AAA','BBB','CCC','DDD']
或
['^ AAA','BBB ^','^ CCC,DDD]
我想这样做,是因为我想从一个HTML页面这样提取文本:
< TD类=细胞1>
< DIV><跨度类=值帧>&安培; NBSP; ~~~~ ^ ABC DEF ^ HGK> LMN ^< / SPAN>< / DIV>
< / TD>
解决方案
您可以这样做:
[x.strip('〜'),用于在re.split X('* [\\ ^ \\]的计算值*',S)如果x]
#[AAA,BBB,CCC,DDD']
I need a Python regex to give me all the strings between ~
and ^
from a string like this:
~~~~ AAA ^ BBB ^ CCC > DDD ^
I've tried this:
import re
target = ' ~~~~ AAA > ^ BBB ^ CCC > DDD ^ '
matchObj = re.findall(r'~(.*?)\^', target)
print matchObj
But the result is:
['~~~ ABC ']
What I expect is:
['AAA', 'BBB', 'CCC', 'DDD']
or
['^AAA', '^BBB', '^CCC', 'DDD']
I want to do this because I am trying to extract text from an HTML page like this:
<td class="cell-1">
<div><span class="value-frame"> ~~~~ ABC ^ DEF ^ HGK > LMN ^</span></div>
</td>
解决方案
You could do this:
[x.strip('~ ') for x in re.split(' *[\^\>] *', s) if x]
# ['AAA', 'BBB', 'CCC', 'DDD']
这篇关于提取使用python 2 chracters之间的串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文