question

user42-8410 avatar image
0 Votes"
user42-8410 asked user42-8410 commented

Regex for finding tags and text around/in between them?

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!

dotnet-csharpc++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered user42-8410 commented

In C# you can do something like this:

 string text = "text1<tag1>text2<tag2>text3";
    
 string[] results = Regex.Split( text, @"(?!^)(?=<\w+>)|(?<=<\w+>)(?!$)" );

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Beautiful, thank you!

0 Votes 0 ·