RickyTad
RickyTad

Reputation: 361

Split string by delimiter and skip parts where delimiter followed by some character

Is it possible to use Regex.Split() to split a string by a delimiter, but only if delimiter not followed by some character. For instance I would like to split a string by the ":" delimiter, but ignore the ":=" delimiter. This code:

string input = "One : Two:Three:=Four";
string pattern = @":";
Regex reg = new Regex(pattern);
var parts = reg.Split(input):

returns 4 parts : "One", "Two", "Three" and "=Four".

I tried to modify the sample like this:

string input = "One : Two:Three:=Four";
string pattern = @":[^=]";
Regex reg = new Regex(pattern);
var parts = reg.Split(input):

Now I get 3 parts: "One", "Two" and "hree:=Four". What has to be changed in the pattern to get also the start of the third part correctly, like this: "Three:=Four".

What about extending the above example with an additional start/end delimiter definition that disables also the split by the defined delimiter. For instance:

string input = "One : Two:Three:=Four {Comment:String:="This is a comment";AddditionalInfo:String:="This is some additional info";}";

The result should contain the following 3 parts: "One", "Two" and "Three:=Four {Comment:String:="This is ...}".

I mean the ":" delimiter should be ignored if present between the "{" and "}" delimiters.

Upvotes: 1

Views: 984

Answers (3)

The fourth bird
The fourth bird

Reputation: 163207

You could use a negative lookbehind (?<! and a negative lookahead (?! to assert that the colon is not between curly braces.

To make sure that the colon is not followed by an equals sign you could also use a negative lookahead.

(?<!\{[^{}]*):(?!=)(?![^{}]*})

Regex demo

Explanation

  • (?<! Negative lookbehind to check what is on the left is not
    • \{[^{}]* Match { followed by matching 0+ times not { or }
  • ) Close non capturing group
  • :(?!=) Match a colon if what follows is not an equals sign
  • (?! Negative lookahead
    • [^{}]*} Match 0+ times not { or } followed by matching }
  • )

Using your example, that would look like:

string input = "One : Two:Three:=Four";
string pattern = @"(?<!\{[^{}]+):(?!=)(?![^{}]+})";
Regex reg = new Regex(pattern);
var parts = reg.Split(input);

Demo C#

Upvotes: 0

Xiaoy312
Xiaoy312

Reputation: 14477

You can split with:

:(?!=)(?![^{]*})

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

Use:

:(?!=)

So your code should be like:

string input = "One : Two:Three:=Four";
string pattern = @":(?!=)";
Regex reg = new Regex(pattern);
var parts = reg.Split(input);

For the second part of your question, you may first choose to remove those between {} and then split them:

input = Regex.Replace(input, @"\{[^}]*\}", "");

Live Demo

Upvotes: 1

Related Questions