Reputation: 7012
I have a following regular expression that I use in C# code that tries to match save some
followed by some word and delete some
followed by some word in a file.
\bsave\ssome\b\s[^\s]+|\bdelete\ssome\b\s[^\s]+
This works as expected. Now, I wanted to exclude those from the match that has a pattern like - save some
and - delete some
.
I tried using the following but it didn't work. In the expression below I just used the expression to ignore - save some
Appreciate any help.
(?!-\s\bsave\ssome\b\s[^\s])(\bsave\ssome\b\s[^\s]+|\bdelete\ssome\b\s[^\s]+)
The demo is here
Upvotes: 3
Views: 1802
Reputation: 626738
You need to use a negative look-behind and group the pattern:
(?<!-\p{Zs}*)(?:\bsave\ssome\b\s[^\s]+|\bdelete\ssome\b\s[^\s]+)
^^^^^^^^^^^^ ^ ^
See regex demo
The (?<!-\p{Zs}*)
lookbehind fails a match if the save/delete
some is preceded with -
followed by zero or more spaces (use +
if there must be at least one). I used \p{Zs}
to only match horizontal whitespace. If you want to match newlines, use \s
.
A contracted regex version:
(?<!-\s*)\b(save|delete)\ssome\b\s\S+
Use the Explicit Capture
flag with it. Since \ssome\b\s\S+
is common for both alternatives, you can move the end of the group to just include save
and delete
.
C#:
var rx = @"(?<!-\s*)\b(save|delete)\ssome\b\s\S+";
Upvotes: 3