C# Regex find string between two strings with newL

2020-02-06 03:02发布

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

That means I want to get the stuff between "start" and "end". But the problem is that between start and end is a new line or \n and the regex doesn't return anything.

So how do I make regex find \n?

3条回答
一纸荒年 Trace。
2楼-- · 2020-02-06 03:22

The name of the Multiline option is misleading, as is the one of the correct option - Singleline:

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

From MSDN, RegexOptions Enumeration:

Singleline - Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

查看更多
冷血范
3楼-- · 2020-02-06 03:23

Include the RegexOptions.SingleLine which means that . matches everything, including \n

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

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

查看更多
家丑人穷心不美
4楼-- · 2020-02-06 03:24

Use Singleline instead of Multiline:

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

BTW, RegexBuddy is your invaluable friend (No, I'm not connected whatsoever to the author, except for being a happy user).

查看更多
登录 后发表回答