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

提取含转义序列的双引号字符串:正则表达式匹配异常求助

Fixing Your Escaped Double Quote Extraction Regex

Let's break down why your current regex isn't behaving as expected, then fix it properly for your use case.

The Problem With Your Current Regex

Your regex "(?:\\"|.)*?" has a critical flaw: the . wildcard matches any character—including unescaped double quotes. Since you're using non-greedy matching (*?), this can lead to early termination of matches or accidental inclusion of unclosed quote segments, which explains why your results aren't lining up with the target strings you want.

The Correct Regex

Instead of matching arbitrary characters, we need to explicitly match either an escaped quote OR any character that isn't a double quote. This ensures we only stop matching when we hit an unescaped closing quote.

Use this regex:

"(?:\\"|[^"])*"

Let's break this down piece by piece:

  • ": Matches the opening double quote
  • (?:\\"|[^"]): A non-capturing group that handles two valid cases inside the quoted string:
    • \\": An escaped double quote (we need to escape the backslash for regex syntax)
    • [^"]: Any character that is not an unescaped double quote
  • *: Repeats the group zero or more times to cover the full content of the quoted string
  • ": Matches the closing unescaped double quote

Testing Against Your Sample Text

For your input string:

This is some text, and here's some in "double quotes" "and here's a double quote:" and some more", "text that follows"

This regex will correctly capture exactly the three target strings you listed:

  1. "double quotes"
  2. "and here's a double quote:\" and some more"
  3. "text that follows"

A Quick Note on Greediness

You'll notice we dropped the non-greedy ? modifier here—and that's intentional. By restricting the inner match to exclude unescaped quotes, the greedy * safely matches all content up to the first valid closing quote, which is exactly the behavior we need. Non-greediness isn't necessary here because we've eliminated the possibility of premature termination.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:27:25