Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string and string arrays

string s= abc**xy**efg**xy**ijk123**xy**lmxno**xy**opq**xy**rstz;

I want the output as string array, where it get splits at "xy". I used

  string[] lines = Regex.Split(s, "xy"); 

here it removes xy. I want array along with xy. So, after I split my string to string array, array should be as below.

lines[0]= abc;
lines[1]= xyefg;
lines[2]= xyijk123;
lines[3]= xylmxno;
lines[4]= xyopq ;
lines[5]= xyrstz;

how can i do this?

like image 431
young Avatar asked Mar 14 '23 19:03

young


1 Answers

(?=xy)

You need to split on 0 width assertion.See demo.

https://regex101.com/r/fM9lY3/50

string strRegex = @"(?=xy)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"abcxyefgxyijk123xylmxnoxyopqxyrstz";

return myRegex.Split(strTargetString);

Output:

abc xyefg xyijk123 xylmxno xyopq xyrstz

like image 134
vks Avatar answered Mar 23 '23 07:03

vks