Ram Cya Ram Cya - 11 months ago 37
ASP.NET (C#) Question

Need to create a Regular expression to Split String after first \r\n

I have been stuck in a situation .

Here are few input strings -




I need the output to be splited like -

"abacuses\r\n25" to be splitted into A)abacuses B)25

"alphabet\r\n56,\r\n57" to be splitted into A)alphabet B)56,57

"animals\r\n44,\r\n45,\r\n47" to be splitted into A)animals B)44,45,47

So far I have tried this but it doesn't work-

string[] ina = Regex.Split(indexname, @"\r\n\D+");

string[] ina = Regex.Split(indexname, @"\r\n\");

Please Help

Answer Source

No regex needed in your example. You basicaly parse string:

string input = "animals\r\n44,\r\n45,\r\n47";
var split = input.Split(new char[]{'\r','\n',','}, StringSplitOptions.RemoveEmptyEntries);

var name = split[0];                        //animals
var args = string.Join(",", split.Skip(1)); //44,45,37

Many people use it for parsing, but Regex is not a parsing language! It is pattern matcher! It is used to find substrings in string! If you can just Split your string - just do it, really. It is much easier to understand than Regex expression.