How to split into two or more blank lines? Regex.Stplit adds unwanted lines

I need to break this line: "hello1\r\nhello2\r\n\r\nhello3\r\n\r\n\r\nhello4" to: {"hello1\r\nhello2" , "hello3", "hello4"}

my code is:

 string text = "hello1\r\nhello2\r\n\r\nhello3\r\n\r\n\r\nhello4"; string[] wordsarray = Regex.Split(text, @"(\r\n){2,}"); 

Result: {"hello1\r\nhello2" ,"\r\n" , "hello3" ,"\r\n" ,"hello4"}

What am I doing wrong?

+6
source share
1 answer

You are very close. Just use a group without capture:

 Regex.Split(text, @"(?:\r\n){2,}") 

Regex.Split adds the captured groups to the result array, as described in the "Remarks" section of Regex.Split .

+8
source

All Articles