Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match certain string and ignore if it starts with some pattern

Tags:

c#

regex

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

like image 368
Sri Reddy Avatar asked Jan 20 '26 05:01

Sri Reddy


1 Answers

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+";
like image 190
Wiktor Stribiżew Avatar answered Jan 23 '26 21:01

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!