I want to split my text by <> characters.
Example suppose I have a string
string Name="this <link> is my <name>";
Now I want to split this so that I have a array of string like
ar[0]="this "
ar[1]="<link>"
ar[2]=" is my "
ar[3]="<name>"
I was trying with split function like
string[] ar=Name.Split('<');
I have also tried
string[] nameArray = Regex.Split(name, "<[^<]+>");
But this is not giving me
"<link>"
and "<name>"
But it is not a good approach.
Can I use regular expression here.
This
gives
(underscores used for clarity)
The regex splits on a zero-width point (so it doesn’t remove anything) which is either:
<>and followed by somethingThe “something” checks are necessary to avoid empty strings at the start or end if your string starts or ends with something in brackets.
Note something like
"<link<link>>"will give you{ "<link", "<link>", ">" }so try to make your angle brackets balance.If you want empty strings if the string starts with
<or ends with>you can use(?=<)|(?<=>). If you want empty strings in the middle when you encounter><, I think you need to first split on(?=<)and then split all the results on(?<=>)– I don’t think you can do it in one go.