如何在正则表达式中查找文本后跟数字

时间:2017-02-19 20:23:08

标签: python regex

我有这个文本代码:

primary- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
personal- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
complete- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
use.- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
light- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
durable- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
repair.- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
rough- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
user(-{'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
ex-nokia- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
many- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
various- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
scratches.-{'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
considerably- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
fast- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
android-- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}
huge- {'neu': 0.0, 'compound': 0.3182, 'pos': 1.0, 'neg': 0.0}
unbeatable- {'neu': 1.0, 'compound': 0.0, 'pos': 0.0, 'neg': 0.0}

我想找到所有出现的负值,就像这样:

0.0
0.1
0.0
1.0

我写了这个正则表达式,但它不起作用:

exp=re.compile('neg'r':(\d).(\d)')

1 个答案:

答案 0 :(得分:1)

使用re.findall()功能:

import re 

with open('lines.txt', 'r') as fh:
    contents = fh.read()
    negs = re.findall(r"'neg':\s?(\d+\.\d+)\}", contents, re.M)

获取具有负值的单词的其他方法

neg_words = re.findall(r"^([-\w.]+)\s?\{[^{]+?(?='neg':\s?-\d+\.\d+\})", contents, re.M)