Sag1v Sag1v - 1 year ago 78
C# Question

Regex Matches in C# get groups of strings that not contains a pattern

I'm trying to get collection of string subsets from a string,
in this example pairs of


Given the string:


I want 2 groups:

That's easy as


But I would like it to be more precise.

Given the string:


I would it to omit the second
in the middle (because I'm searching for open and closing tags).

I want this result:


I've tried to create a lookahead or lookbehind but no luck (I'm sure I'm using it wrong):


Answer Source

I assume the <tag> and </tag> are used as an example as leading/trailing delimiters.

Note that the lazy dot matching will still match from the first leading delimiter till the first occurrence of the trailing delimiter including any occurrences of the leading one.

To work around it, use a tempered greedy token:


See the regex demo

Since the lookahead is executed at each position, this construct is rather resource consuming. You can unroll it as


See another regex demo.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download