首先匹配,然后用正则表达式匹配所有相等的事件

Match first and then all equal occurrences with regex

假设我们有字符串:

one day, when Anne, Lisa and Paul went to the store, then Anne said to Paul: "I love Lisa!". Then Lisa laughed and kissed Anne.

有没有办法用正则表达式匹配名字,然后匹配字符串中所有其他出现的同名?

给定名称匹配正则表达式 /[A-Z][a-z]+(也许 /g?),是否可以让正则表达式匹配器记住第一个匹配项,然后在其余匹配项中完全使用该匹配项细绳?应忽略名称匹配正则表达式的其他后续匹配项(示例中的 Anne 除外)。

结果将是(如果匹配被替换为“Foo”):

one day, when Foo, Lisa and Paul went to the store, then Foo said to Paul: "I love Lisa!". Then Lisa laughed and kissed Foo.

请忽略句子开头未大写的事实,或添加一个同样处理此问题的示例。

使用脚本获取第一个匹配项,然后将其用作第二次迭代的输入当然可行,但这超出了问题的范围(仅限于一个正则表达式)。

我能想到的唯一方法是 non-fixed 宽度后视。例如通过 Pypi 的正则表达式模块,也许 Javascript 也是?无论哪种方式,假设根据您的问题通过 [A-Z][a-z]+ 捕获名称,请尝试:

\b([A-Z][a-z]+)\b(?<=^[^A-Z]*\b\b.*)

在线查看demo


  • \b([A-Z][a-z]+)\b - 第一个捕获组捕获两个 word-boundaries;
  • 之间的名称
  • (?<=^[^A-Z]*\b\b.*) - non-fixed 宽度正后视以匹配行锚点的开头,后跟大写字母以外的 0+ 个字符,后跟第一个捕获组的内容和 0+ 个字符。

这是一个 PyPi 的例子:

import regex as re

s= 'Anne, Lisa and Paul went to the store, then Anne said to Paul: "I love Lisa!". Then Lisa laughed and kissed Anne.'
s_new = re.sub(r'\b([A-Z][a-z]+)\b(?<=^[^A-Z]*\b\b.*)', 'Foo', s)
print(s_new)

打印:

Foo, Lisa and Paul went to the store, then Foo said to Paul: "I love Lisa!". Then Lisa laughed and kissed Foo.