正则表达式技术问询:匹配指定关键词且不含特定修饰词的目标行
Fixing Your Regex to Only Match Case 2
Got it, let's tweak that regex so it only picks up Case 2 and excludes the other two. Here's the breakdown and solution:
The Core Problem
Your current regex matches any line starting with expect or project that includes the target EPS phrase—but we need to:
- Only target lines starting with
project/Projected(excludeexpectentirely) - Exclude lines that contain
non-GAAPoradjusted(since those are present in Cases 1 and 3)
The Updated Regex
(?i)^(?:project)(?!.*(non-GAAP|adjusted)).*(diluted\sEPS\sfrom\scontinuing\soperations)$
Let's Break It Down
(?i): Optional but handy—turns on case-insensitive matching, so it works for bothProjectedandproject^: Anchors the match to the start of the line(?:project): Matches the required starting word (non-capturing group, so it doesn't clutter your match results)(?!.*(non-GAAP|adjusted)): Negative lookahead—this ensures the line does NOT contain eithernon-GAAPoradjustedanywhere in it.*: Matches any characters in between (use.*?instead if you want non-greedy matching, though it's not strictly necessary here)(diluted\sEPS\sfrom\scontinuing\soperations): Your target EPS phrase, exactly as you need it$: Anchors the match to the end of the line, ensuring we're matching full lines
Why This Works for Your Cases
- Case 1: Starts with
expect—doesn't match theprojectopening, so it's excluded - Case 2: Starts with
Projected, has nonon-GAAP/adjusted, and includes the EPS phrase—gets matched - Case 3: Contains
adjusted—the negative lookahead blocks the match
Quick Note
If you're using a tool that doesn't support (?i) for case insensitivity, you can replace (?:project) with (?:Project|project) instead.
内容的提问来源于stack exchange,提问作者CHANDRA SUDHEER K
相关产品推荐
相关产品推荐

