使用正则表达式提取带十进制数的整个句子

时间:2017-12-11 01:35:49

标签: python regex string findall

我正在尝试使用正则表达式来提取包含单词" R& D"的句子。 我的代码是

**\s[^.?!]*R&D[^.?!]*[?.!]**

我希望得到The third quarter R&D spending was 2.4 million, up approximately 100,000 from Q2 levels. SG&A expenses were 3.4 million, down 200,000 from Q2.

我得到了The third quarter R&D spending was 2.

1 个答案:

答案 0 :(得分:1)

您可以将R&D[\w\W]包围,以匹配所有字母数字和非字母数字字符:

import re
s = "The third quarter R&D spending was 2.4 million, up approximately 100,000 from Q2 levels. SG&A expenses were 3.4 million, down 200,000 from Q2."
sentence = re.findall('^[\w\W]+R&D[\w\W]+$', s)[0]

输出:

'The third quarter R&D spending was 2.4 million, up approximately 100,000 from Q2 levels. SG&A expenses were 3.4 million, down 200,000 from Q2.'