C # Regex find line between two lines using newLine

Here is my regex: Regex r = new Regex("start(.*?)end", RegexOptions.Multiline);

This means that I want to get material between "start"and "end". But the problem is that between the beginning and the end there is a new line or \n, and the regex does not return anything.

So how do I find regex find \n?

+5
source share
3 answers

The parameter name Multilineis misleading, as is the correct option - Singleline:

Regex r = new Regex("start(.*?)end", RegexOptions.Singleline);

From MSDN, Listing RegexOptions :

Singleline - sets single line mode. Changes the value of a dot (.) So that it matches each character (instead of every character except \ n).

+5
source

RegexOptions.SingleLine, , . , \n

Regex r = new Regex("start(.*?)end", RegexOptions.Multiline | RegexOptions.SingleLine);

. http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx.

+1

Use Singleline instead of Multiline:

Regex r = new Regex("start(.*?)end", RegexOptions.Singleline);

BTW, RegexBuddy is your invaluable friend (no, I'm not connected with the author, except to be a happy user).

+1
source

All Articles