RegEx:在引号内捕获单词

时间:2018-09-17 01:57:09

标签: python regex regex-group

我有一段这样的文字:

  

约翰出去散步。他遇到了爱德华兹夫人,并说:“您好,妈妈,您今天好吗?”。她回答:“我很好。你好吗?'。

我想用单引号引起来。 我尝试过此正则表达式

re.findall(r"(?<=([']\b))((?=(\\?))\2.)*?(?=\1))",string)

(来自这个问题:RegEx: Grabbing values between quotation marks

它仅返回单引号作为输出。我不知道出了什么问题有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

Python要求捕获组必须完全关闭,然后才能对该组进行任何反向引用(\2)。

您可以使用Positive Lookbehind (?<=[\s,.])Positive Lookahead (?=[\s,.])零长度断言来匹配单引号内的单词,包括I'm之类的单词,即:

re.findall(r"(?<=[\s,.])'.*?'(?=[\s,.])", string)

Full match  56-92   'Hello Mam how are you doing today?'
Full match  106-130 'I'm fine. How are you?'

说明 enter image description here


Regex Demo

相关问题