1

我想提取所有具有以下标签的单词"w="。例如,我需要," THAT HAVE RECEIVED NO"从下面的字符串。

w="THAT" v="22.23092" a="19.09109" i="3"/>
<r s="1480150" d="150" w="HAVE" v="20.66713" a="19.09183" i="3"/>
<r s="1480300" d="360" w="RECEIVED" v="18.70063" a="19.09165" i="2"/>
<r s="1480660" d="200" w="-SIL-" v="11.65527" a="19.09165" i="0"/>
<r s="1480860" d="210" w="NO" v="18.49828" a="19.09137" i="2"/>
<r s="1481070" d="4330" w="-S-" v="11.55029" a="19.09137" i="0"/>
<r s="1485400" d="4170" w="-S-" v="11.88606" a="19.09137" i="0"/>

我一直在尝试使用以下正则表达式:

 matches = re.findall('(?<=[w][=])\w+',line)

但是,它似乎不起作用。请帮忙。

4

2 回答 2

1

像这样的东西:

>>> import re
>>> re.findall(r'w="(\w+)"',strs,re.DOTALL)
['THAT', 'HAVE', 'RECEIVED', 'NO']

然后用于str.join获取单个字符串:

>>> " ".join(re.findall(r'w="(\w+)"',strs,re.DOTALL))
'THAT HAVE RECEIVED NO'

哪里strs是:

>>> print strs
w="THAT" v="22.23092" a="19.09109" i="3"/>
<r s="1480150" d="150" w="HAVE" v="20.66713" a="19.09183" i="3"/>
<r s="1480300" d="360" w="RECEIVED" v="18.70063" a="19.09165" i="2"/>
<r s="1480660" d="200" w="-SIL-" v="11.65527" a="19.09165" i="0"/>
<r s="1480860" d="210" w="NO" v="18.49828" a="19.09137" i="2"/>
<r s="1481070" d="4330" w="-S-" v="11.55029" a="19.09137" i="0"/>
<r s="1485400" d="4170" w="-S-" v="11.88606" a="19.09137" i="0"/>
于 2013-07-11T20:47:05.587 回答
0

你想要更像的东西吗

re.findall('(w=")([^"]*)(")', line)

?

于 2013-07-11T20:47:58.303 回答