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

正则表达式需求:匹配含red不含ball的行及反向匹配规则

Regex Solutions for Line-Level Matching

Got it, let's break down your regex needs with line-specific patterns tailored exactly to what you're asking for:

1. Match lines containing "red" but NOT "ball" (order doesn't matter)

Use this regex to capture any full line that has "red" anywhere, but never includes "ball":

^(?=.*red)(?!.*ball).*$

Quick breakdown:

  • ^ anchors the match to the start of the line
  • (?=.*red): Positive lookahead that checks if "red" exists anywhere in the line (.*/ covers any characters before it)
  • (?!.*ball): Negative lookahead that guarantees "ball" does NOT show up anywhere in the line
  • .*$ matches all remaining characters from the current spot to the end of the line

Which of your examples match?

I have a red cup
This cup that I have is red

These lines have "red" and no trace of "ball" — they’ll get captured perfectly.


2. Match lines containing BOTH "red" and "ball" (order doesn't matter)

This is the reverse rule, grabbing lines that include both terms no matter which comes first:

^(?=.*red)(?=.*ball).*$

Quick breakdown:

  • ^ start of line anchor
  • (?=.*red): Makes sure "red" is present in the line
  • (?=.*ball): Makes sure "ball" is also present in the line
  • .*$ matches the entire line content

Which of your examples match?

I have a red ball

This is the only line from your samples that includes both "red" and "ball" — it’s the one this regex will target.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:08:19