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

Python正则表达式灾难性回溯问题:提取sale(s)前的大写开头企业名

Fixing Catastrophic Backtracking in Regex for Extracting Company Names

Hey there! Let's work through this regex problem step by step. The catastrophic backtracking you're seeing comes from nested repeating groups in your original pattern—those overlapping + quantifiers force the regex engine to try way too many matching combinations. Let's build a cleaner, more efficient solution that fits your exact needs.

Understanding Your Requirements

First, let's recap what you need to match:

  • Company names starting with an uppercase letter
  • Names can include uppercase/lowercase letters, numbers, -, and '
  • Names might have multiple parts joined by and or &
  • We need to capture the name immediately preceding sale, Sales, Sale, or sales

The Improved Regex Pattern

Here's a streamlined pattern that avoids backtracking and hits all your requirements:

\b([A-Z][a-zA-Z0-9'-]*\.?(?:\s+(?:and|&)\s+[A-Z][a-zA-Z0-9'-]*\.?)*)(?=\s+(?:[Ss]ales?))

Breaking Down the Pattern

Let's break down each component to make it clear:

  • \b: Ensures we start matching at a word boundary, so we don't grab partial words from the middle of a string.
  • [A-Z][a-zA-Z0-9'-]*\.?: Matches a single "name unit"—starts with uppercase, followed by allowed characters, and an optional period (for abbreviations like Corp.).
  • (?:\s+(?:and|&)\s+[A-Z][a-zA-Z0-9'-]*\.?)*: A non-capturing group that handles multi-part names. It matches:
    • One or more spaces (\s+)
    • Either and or &
    • Another name unit (same format as the first one)
    • The * means this can repeat 0 or more times (so single-word names work too)
  • (?=\s+(?:[Ss]ales?)): A positive lookahead that checks for the trailing sale(s)/Sale(s) without including it in the captured group. This keeps our result clean and avoids unnecessary backtracking.

Testing with Your Example Text

Let's apply this regex to your sample text:

;;;;;PRINCIPAL CUSTOMERS In fiscal 2005, the Company derived approximately 21% ($4,782,852) of its consolidated revenues from continuing operations from direct transactions with Kmart Corporation. Sales of Computer products was good. However, Computer's Parts and Display Segment sale has been decreasing.

It will correctly capture Computer's Parts and Display Segment—exactly what you need.

Why Your Original Pattern Caused Backtracking

Your original regex had nested repeating groups like ((?:(?:...)?)+)+?, which creates ambiguity for the regex engine. For longer strings, it has to try thousands of possible ways to split the text between the nested + quantifiers, leading to that catastrophic backtracking error. The new pattern uses a flat, linear structure that the engine can process efficiently without redundant checks.

Optional Adjustments

If you need to handle edge cases like:

  • No space before sale(s) (unlikely per your example, but possible): Modify the lookahead to (?=\s*[Ss]ales?) (allow zero or more spaces)
  • Names with hyphenated parts (like Smith-Jones Co.): The pattern already handles -, so no changes needed

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:52:19