3,973 questions
In C# you can do something like this:
string text = "text1<tag1>text2<tag2>text3";
string[] results = Regex.Split( text, @"(?!^)(?=<\w+>)|(?<=<\w+>)(?!$)" );
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a text that is a mix of "tags" defined as (<[^<>]+>) and "non-tags", I'd like to separate them into an array. This is for colorizing text in a small code editor.
Here are some examples of input and what I'd like to have as the output:
"text1<tag1>text2<tag2>text3" -> ["text1", "<tag1>", "text2", "<tag2>", "text3"]
"<tag1>text text<tag2><tag3>" -> ["<tag1>", "text text", "<tag2>", "<tag3>"]
"<tag1><tag2>" -> ["<tag1>", "<tag2>"]
"text text" -> ["text text"]
"<<<tag1>text>><<<tag2>" -> ["<<", "<tag1>", "text>><<", "<tag2>"]
I assume that is something Regex can do?
Thank you!
In C# you can do something like this:
string text = "text1<tag1>text2<tag2>text3";
string[] results = Regex.Split( text, @"(?!^)(?=<\w+>)|(?<=<\w+>)(?!$)" );