I need two C# Regular expressions to split using space but exclude text between [: and :] special characters.
Example: 1st Case:
string input1 = "one [two two] three [:four four:] five six [seven seven:] [:eight eight]";
output:
one
[two
two]
three
[:four four:]
five
six
[seven
seven:]
[:eight
eight]
Example: 2nd Case:
output:
one
[two two]
three
[:four four:]
five
six
[seven
seven:]
[:eight
eight]
I tried this but not working, producing below output
string input1 = "one [two two] three [:four four:] five six [seven seven:] [:eight eight]";
var parts1 = Regex.Matches(input1, @"[[::]].+?[\[::]]|[^ ]+").Cast<Match>()
.Select(m => m.Value)
.ToArray();
one
[two two] three [:four four:]
five
six
[seven seven:]
[:eight
eight]
You can use
var pattern = @"\[:[^][]*:]|\[(?!:)[^][]*(?<!:)]|\S+";
var results = Regex.Matches(text, pattern).Cast<Match>().Select(x => x.Value);
See the regex demo.
Details:
\[:
- [:
string[^][]*
- zero or more chars other than [
and ]
:]
|
- or\[
- a [
char(?!:)
- immediately on the right, there should be no :
[^][]*
- zero or more chars other than [
and ]
(?<!:)]
- a ]
char that has no :
right before it|
- or\S+
- one or more non-whitespace chars.