如何用JavaScript正则表达式实现嵌套括号的匹配与提取
Extract Nested Parentheses by Position (Stack-Based Approach)
Got it, when you need to pull content starting from the second opening parenthesis up to its matching closing pair—without relying on content-specific regex—a stack-based method is your best bet. It handles nested parentheses flawlessly by tracking the depth of each opening/closing bracket, so you don’t have to worry about what’s inside the brackets.
Step-by-Step Breakdown:
- Pinpoint your starting index: First, skip the very first
(in your text, then grab the index of the next opening parenthesis—this is where your target content begins. - Track parenthesis depth: Use a counter (a simple stand-in for a stack) to keep track of how many nested parentheses you’re inside:
- Add 1 to the counter every time you hit an opening
(. - Subtract 1 every time you hit a closing
).
- Add 1 to the counter every time you hit an opening
- Find the matching closing bracket: When the counter drops back to 0, you’ve reached the exact closing parenthesis that pairs with your starting second
(. - Extract the content: Slice the text from your starting index up to and including this matching closing bracket.
Example Code (Python):
Here’s a practical implementation that works with your sample texts:
def extract_from_second_parenthesis(text): # Locate the second opening parenthesis first_paren = text.find('(') second_paren = text.find('(', first_paren + 1) if second_paren == -1: return None # No second parenthesis exists # Traverse to find the matching closing parenthesis depth = 1 for idx in range(second_paren + 1, len(text)): if text[idx] == '(': depth += 1 elif text[idx] == ')': depth -= 1 if depth == 0: # Return the full matched parentheses block return text[second_paren:idx+1] return None # No matching closing parenthesis found # Test with your first sample text sample_text = """(ti,ab(((Abbott near/10 (assay* OR test* OR analy* OR <em>array</em>)) OR (Abbott p/1 Point P/1 Care) OR ARCHITECT OR (CELL p/0 DYN)) OR ((Alere near/10 (assay* OR test* OR analy* OR <em>array</em>)) OR (Alere NEAR/5 (Triage P/1 System)) OR INRatio OR Afinion) OR ((Beckman* p/1 Coulter near/10 (assay* OR test* OR analy* OR <em>array</em>)) OR ((Beckman* p/0 Coulter) near/2 AU????) OR (UniCel* P/1 DxC) OR (UniCel* p/1 DxI) OR ( Beckman* near/5 Access) OR (Access* p/1 Systeme) OR (CytoFLEX OR (cyto p/0 flex)) OR (UniCel* p/1 DxH) OR ((Coulter* p/1 LH) OR Coulter<em>LH)) OR ((Ortho p/0 Clinical P/1 Diagnostics) OR VITROS OR (vitros</em> p/1 System*) OR (VITROS* p/1 ECiQ) OR ORTHOTM OR (orthotm p/1 VISION) OR (ORTHO p/1 AutoVue*)) OR ((Instrumentation p/0 Laboratories) OR HemosIL OR ACLTOP OR (ACL p/0 ELITE) OR (GEM* P/1 Premier) OR GEM<em>OPL) OR ((Radiometer near/10 (assay</em> OR test* OR analy* OR <em>array</em>)) OR (AQT?? p/0 FLEX) OR (ABL?? p/0 FLEX) OR HemoCue*) OR ((Nova p/0 Biomedical) OR StatStrip OR (STAT p/0 PROFILE*) OR ((Nova p/0 Biomedical) near/1 Prime) OR STATPROFILE*) OR (((Siemens p/0 Healthcare) near/10 (assay* OR test* OR analy* OR <em>array</em>)) OR (ADVIA p/0 Centaur) OR (Dimension p/0 Vista) OR RAPIDPOINT))) and (ud(>20170101)) (see attachment)""" extracted_content = extract_from_second_parenthesis(sample_text) print(extracted_content)
Why This Works:
- The depth counter ensures we don’t stop at the first random closing bracket—we only stop when every nested opening parenthesis has been matched.
- It’s completely content-agnostic, so it works no matter what text is inside the parentheses, even if the content changes every time.
Test It Out:
Run this code against any of your sample texts, and it’ll pull exactly the highlighted section you’re targeting—no guesswork with regex required.
内容的提问来源于stack exchange,提问作者Code Guy




