I’m trying to get paragraphs from a string in C# with Regular Expressions.
By paragraphs; I mean string blocks ending with double or more \r\n. (NOT HTML paragraphs <p>)…
Here is a sample text:
For example this is a paragraph with a carriage return here
and a new line here.At this point, second paragraph starts. A paragraph ends if double or more \r\n is matched or
if reached at the end of the string ($).
I tried the pattern:
Regex regex = new Regex(@"(.*)(?:(\r\n){2,}|\r{2,}|\n{2,}|$)", RegexOptions.Multiline);
but this does not work. It matches every line ending with a single \r\n. What I need is to get all characters including single carriage returns and newline chars till reached a double \r\n.
.*is being greedy and consuming as much as it can. Your second set of()has a$so the expression that is being used is(.*)(?). In order to make the.*not be greedy, follow it with a?.When you specify RegexOptions.Multiline, .NET will split the input on line breaks. Use RegexOptions.Singleline to make it treat the entire input as one.