Perl 兼容的正则表达式 select 除第二个单词外的所有内容

Perl-compatible regular expression to select everything except second word

我正在尝试使用 PCRE select 除了一行文本中的第一个数字字符串之外的所有内容。数字字符串可以是 3、4 或 5 位数字,但始终是文章之后的第二个单词。以下是一些示例:

article 11367 airline experts incheon airport transfers operational expertise indonesia

article 8364 future investment korean government invest 4 trillion won science technology ict rd

article 2151 fighter procurement lockheed martin able propose lower prices f 35s

我目前使用的表达方式是\b(?=\w*[a-zA-Z])\w+\b。这 select 除了数字之外的一切。这几乎是完美的,但是正如您在上面的第 2 行和第 3 行中看到的那样,有时数字会出现在第一组之后。我想 select 除第二个单词之外的每个单词,或者忽略第一组数字但包括其他数字,以便最终匹配的结果如下所示:

article airline experts incheon airport transfers operational expertise indonesia

article future investment korean government invest 4 trillion won science technology ict rd

article fighter procurement lockheed martin able propose lower prices f 35s

我这样做是因为 Drupal 模块 search404 在其配置中包含一个 PCRE 字符串,select 包含您想要从搜索字符串中消除 的所有内容。它还包括使用搜索词自动执行自定义搜索 URL 的功能,并允许您构建 URL。我想构建 URL 使其看起来像 /node/number,这将自动将用户从旧文章 URL(上面的行之一)发送到新文章 URL.

更新

如果您确定数字排在第二位,请使用 \K

^\S+\s+|\G(?!^)\d+\K|\G(?!^).*$

替换为 empty string。参见演示。

https://regex101.com/r/fX3mH8/3



原版post

如果您确定数字排在第二位,请使用 \K

^\S+\s+\K\d+

替换为 empty string。参见演示。

https://regex101.com/r/fX3mH8/1