根据正则表达式提取字符串

时间:2019-02-04 08:02:50

标签: python regex

假设我有一个字符串变量

msg = "The issue is Keys on the keyboard are working intermittently . is it working? Was the Keyboard replaced earlier ?"

我想提取“。”之间的所有句子。和“?”还“?”和“?”

预期输出:

["is it working",  "Was the Keyboard replaced earlier "]

我正在尝试这种模式,但没有获得预期的输出

re.findall('(?<=\.).*(?=\?)',s)

更新后的字符串

  

msg =“。问题是键盘上的键间歇性地工作   。工作正常吗?键盘更早更换了吗?“

2 个答案:

答案 0 :(得分:0)

您需要惰性匹配。
所以正则表达式应该是这个。
(?<=\.|\?).*?\?

您显示给我的正则表达式未捕获问号起始句。


测试字符串:
The issue is Keys on the keyboard are working intermittently . is it working? Was the Keyboard replaced earlier ?

正则表达式字符串:
(?<=\.|\?).*?\?

结果:
is it working?
Was the Keyboard replaced earlier ?

答案 1 :(得分:0)

[!@#$%^&*(),。?“:{} | <>]尝试使用此正则表达式以获得预期的模式。.我认为您没有使用正确的正则表达式。在字符串中找到特殊字符