Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array of strings from a larger string after delimiting

I have a string input which looks like this var input = "AB-PQ-EF=CD-IJ=XY-JK". I want to know if there is a way using string.split() method in C# and LINQ such that I can get an array of strings which looks like this var output = ["AB-PQ", "PQ-EF", "EF=CD", "CD-IJ", "IJ=XY", "XY-JK"]. Currently I am doing the same conversion manually by iterating the input string.

like image 850
pango89 Avatar asked Jul 13 '18 11:07

pango89


1 Answers

Can you use a regex instead of split?

var input = "AB-PQ-EF=CD-IJ=XY-JK";
var pattern = new Regex(@"(?<![A-Z])(?=([A-Z]+[=-][A-Z]+))");
var output = pattern.Matches(input).Cast<Match>().Select(m => m.Groups[1].Value).ToArray();
like image 196
Rawling Avatar answered Oct 19 '22 19:10

Rawling