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

正则表达式需求:匹配指定单词间不含特定字符串的内容

Solution for Your Regex Query

Your goal is to capture all substrings starting with The , ending with blue, and containing no cat in between. The regex you tried (The.*?(?!cat)blue) fails because the negative lookahead only checks right before "blue"—it doesn’t block "cat" from appearing earlier in the substring.

Correct Regex

Here’s a regex that meets your requirements:

The (?:(?!cat).)*? blue 

Breakdown of the Regex

Let’s break down why this works:

  • The : Matches the exact starting sequence.
  • (?:(?!cat).)*?: This non-capturing group ensures "cat" never appears in the middle:
    • (?!cat): Negative lookahead that checks the next three characters aren’t "cat".
    • .: Matches any single character (only if the lookahead passes).
    • *?: Non-greedy quantifier that stops immediately when it hits blue, preventing over-matching.
  • blue: Matches the exact ending sequence.

Testing with Your Example String

For your input string:

Dandelion The animal dog is blue The animal cat is blue Alcohol The animal cow is blue water

This regex will capture the two desired matches:

  1. The animal dog is blue
  2. The animal cow is blue

If you want to exclude the trailing space after "blue", adjust the regex to The (?:(?!cat).)*?blue (remove the space before blue), but ensure it aligns with your exact string structure.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:11:25