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

Java中String.replaceAll遇StackOverflowError的正则优化问询

Fix StackOverflowError When Replacing 'OR' Outside Quotes in Long Strings with Java Regex

The StackOverflowError you're hitting comes from excessive backtracking caused by your original regex's positive lookahead. The pattern (?=(?:"[^"]*"|[^"])*$) uses a nested alternation inside a repeated group, which creates tons of backtracking points when processing long strings—Java's regex engine simply runs out of stack space handling all those possibilities.

Improved Regex Solution

We can rewrite the lookahead to use a more efficient, linear structure that avoids unnecessary backtracking. Here's the fixed pattern:

String replacedStr = inputStr.replaceAll("\\bOR\\b(?=(?:[^"]*+"[^"]*+")*+[^"]*+$)", "||");

What Changed & Why It Works

  • Removed nested alternation: Instead of alternating between quoted segments and non-quoted characters, we first match any number of non-quoted characters, then a full quoted pair ("[^"]*+"), and repeat this pattern. This creates a linear path for the regex engine to follow.
  • Used possessive quantifiers (+): The + after [^"]* and the outer group tells the regex engine to "lock in" matches without saving backtracking points. This eliminates the stack-heavy backtracking that caused your overflow.
  • Simplified lookahead logic: The lookahead now checks that, from the current position, the remaining string has an even number of quotes (meaning we're outside a quoted segment).

Test with Your Sample Input

Using your long input string, this regex will safely replace all standalone ORs outside quotes without throwing a StackOverflowError. For example, the ORs inside paths like "**\\acad.exe" remain untouched, while the logical ORs between conditions get replaced with ||.

Alternative: Manual Parsing (If Regex Still Struggles)

If you're dealing with extremely large strings (10k+ characters), you might want to consider a manual parsing approach instead of regex. This gives you full control over the process and avoids regex engine limitations:

StringBuilder sb = new StringBuilder();
boolean inQuote = false;
int i = 0;
int len = inputStr.length();

while (i < len) {
    // Check if we're entering/exiting a quote
    if (inputStr.charAt(i) == '"') {
        inQuote = !inQuote;
        sb.append('"');
        i++;
        continue;
    }
    
    // Check for standalone "OR" when not in quotes
    if (!inQuote && i + 2 <= len && inputStr.substring(i, i+2).equals("OR")) {
        // Verify it's a standalone word (word boundaries)
        boolean leftBoundary = i == 0 || !Character.isLetterOrDigit(inputStr.charAt(i-1));
        boolean rightBoundary = i+2 == len || !Character.isLetterOrDigit(inputStr.charAt(i+2));
        if (leftBoundary && rightBoundary) {
            sb.append("||");
            i += 2;
            continue;
        }
    }
    
    // Append the current character
    sb.append(inputStr.charAt(i));
    i++;
}

String replacedStr = sb.toString();

This approach iterates through the string once, tracks whether we're inside quotes, and only replaces OR when it's a standalone word outside quotes. It's more verbose but completely avoids regex stack issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:41:12