用正则表达式提取带小数点的整句话

2024-09-29 19:06:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用正则表达式来提取一个包含单词“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.


Tags: thefrom密码sg单词句子levelsup
1条回答
网友
1楼 · 发布于 2024-09-29 19:06:16

可以用[\w\W]环绕R&D,以匹配所有字母数字和非字母数字字符:

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.'

相关问题 更多 >

    热门问题