正则表达式匹配异常:无匹配结果,求提取目标内容的正确表达式
Got it, let's work through this regex problem step by step. Your goal is to capture everything from the start marker :[]: up to and including the first colon that comes after it—here are two reliable ways to do this:
Option 1: Non-Greedy Matching (Simple & Readable)
Use this regex pattern:
:[]:\s*.*?:
Breakdown:
:[]:: Explicitly matches your fixed starting marker.\s*: Matches any number of whitespace characters (including zero) right after the marker, handling cases where there's no space or multiple spaces..*?: The non-greedy version of.*—it matches any characters but stops at the first occurrence of the next part of the pattern.:: Matches the first colon that follows the marker, which is our stopping point.
Option 2: Exclusion Character Set (More Efficient)
If you want a pattern that avoids regex backtracking (better for large documents), use this instead:
:[]:\s*[^:]+:
Breakdown:
:[]:&\s*: Same as above—matches the start marker and any leading whitespace.[^:]+: Matches one or more characters that are NOT colons. This directly stops at the first colon, no need for non-greedy logic.:: Matches the closing colon we want to include.
Why Your Previous Patterns Failed:
- Your first pattern
\B:\[\]:.*:\Bused.*, which is greedy—it matches as much as possible, so it would run all the way to the last colon in the section instead of the first. - Your second pattern
\B:\[\]:\s*.:{1}only matches a single character after the whitespace (the.), which is why it didn't find matches for longer text likeAbet1, Abetted34.
Test with Your Examples:
For your first sample line:
:[]: Abet1, Abetted34: Find the usage in table under section 1-CB-45: Or more info from the related section starting with PARTIE-DU-CORPS.
Both patterns will correctly capture: :[]: Abet1, Abetted34:
For your second sample line:
:[]: Ou est-ce que tu a mal: Tu as mal aux jambes: Find usage in section 145-TT-LA-TETE.
They'll capture: :[]: Ou est-ce que tu a mal:
内容的提问来源于stack exchange,提问作者Darvin

