You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Perl正则表达式需求:匹配文本中三个目标字符串的最后出现实例

问题需求

我需要用支持Perl正则表达式的Doc Parser工具创建文本解析规则,目标是匹配文本中三个指定字符串——i am sitting with after this meeting are、won't be included in your published notes、Single Signal——的最后出现实例。此前在Regex101(PCRE2)中验证有效的表达式,在Doc Parser中无法生效,尝试过添加.*?、+、{1}等变体也没用,现寻求可行的Perl正则解决方案。

输入文本示例

示例1

Single Signal

Two things I am sitting with after this meeting are...

- Words words words

示例2

Single Signal

- words words words

示例3

Single Signal

words words that end in won't be included in your published notes.

- words
已尝试的表达式
  • 核心表达式:
(?i)(i am sitting with after this meeting are|This is for internal use and won't be included in your published notes|Single Signal)
  • 其他变体:添加.*?、+、{1}的调整版本
  • 在Regex101生效但Doc Parser失效的表达式:
(?i)[^(i am sitting with after this meeting are|won't be included in your published notes|Single Signal)]+$
可行正则方案

方案1:单行模式+贪婪匹配(兼容性最优)

(?s)(?i).*\b(i am sitting with after this meeting are|won't be included in your published notes|Single Signal)\b

规则说明:

  • (?s):开启单行模式,让.能匹配换行符,适配跨多行的文本结构
  • (?i):忽略大小写,匹配时不区分字母大小写
  • .*:贪婪匹配任意字符到文本末尾,确保后续捕获的是最后出现的目标字符串
  • \b:单词边界,避免部分匹配(比如防止把包含目标字符串的长单词误识别)

方案2:正向反向断言(需Doc Parser支持)

如果Doc Parser支持无限长度反向断言,也可使用:

(?i)(?<=.*)(i am sitting with after this meeting are|won't be included in your published notes|Single Signal)

注意:部分Perl正则实现对反向断言的长度有限制,方案1的兼容性更强。

匹配结果验证

对应三个示例的匹配结果:

  • 示例1:捕获i am sitting with after this meeting are
  • 示例2:捕获Single Signal
  • 示例3:捕获won't be included in your published notes

内容的提问来源于stack exchange,提问作者April Urban

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 18:53:22