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

如何编写以root cause开头、以category或换行符结束的regex?

匹配需求的正则表达式

可直接使用如下正则满足你的匹配规则:

root cause.*?(?=category|\n)

正则逻辑说明

  • 开头的root cause固定匹配起始关键词
  • .*?为惰性匹配规则,会尽可能短的匹配后续字符,避免出现过度匹配的问题
  • (?=category|\n)是正向零宽断言,不会将匹配到的category或者换行符纳入最终匹配结果,同时会在这两个标识第一次出现时就立刻终止匹配

效果验证

  1. 无换行测试文本验证
    原文本:Hi there how are you. This is a test for the root cause of ice cream addiction and not the category of happiness.
    匹配结果:root cause of ice cream addiction and not the ,完全符合预期
  2. 带换行测试文本验证
    原文本:
    Hi there how are you. This is a test for the root cause of ice cream addiction
    and not the category of happiness.
    
    匹配结果:root cause of ice cream addiction,完全符合预期

补充说明

如果你使用的正则引擎默认设置为.不匹配换行符,也可以将正则简化为root cause.*?(?=category),默认规则下.本身不会越过换行符匹配,也能达到相同的效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 19:06:00