Search code examples
c#arraysregexsplitspecial-characters

C# Regex to split using space but exclude text between [: and :] special characters


I need two C# Regular expressions to split using space but exclude text between [: and :] special characters.

  1. Need regex to split text but exclude text between [: and :] special characters.
  2. Need regex to split text but exclude text between [: and :] and also the 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]

Solution

  • 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.