简体   繁体   English

C# 正则表达式在两个字符串之间用 newLine 查找字符串

[英]C# Regex find string between two strings with newLine

Here is my regex: Regex r = new Regex("start(.*?)end", RegexOptions.Multiline);这是我的正则表达式: Regex r = new Regex("start(.*?)end", RegexOptions.Multiline);

That means I want to get the stuff between "start" and "end" .这意味着我想得到"start""end"之间的东西。 But the problem is that between start and end is a new line or \\n and the regex doesn't return anything.但问题是开始和结束之间是一个新行或\\n并且正则表达式不返回任何内容。

So how do I make regex find \\n ?那么如何让正则表达式找到\\n呢?

The name of the Multiline option is misleading, as is the one of the correct option - Singleline : Multiline选项的名称具有误导性,正确选项之一 - Singleline

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

From MSDN, RegexOptions Enumeration :从 MSDN, RegexOptions 枚举

Singleline - Specifies single-line mode. Singleline - 指定单行模式。 Changes the meaning of the dot (.) so it matches every character (instead of every character except \\n).更改点 (.) 的含义,使其匹配每个字符(而不是除 \\n 之外的每个字符)。

Include the RegexOptions.SingleLine which means that .包括 RegexOptions.SingleLine 这意味着. matches everything, including \\n匹配所有内容,包括\\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.有关更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx

Use Singleline instead of Multiline :使用Singleline而不是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).顺便说一句, RegexBuddy是您宝贵的朋友(不,我与作者没有任何联系,除了是一个快乐的用户)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM