我正在尝试使用正则表达式来提取包含单词" 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.
答案 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.'