C#中的多行正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1780009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:46:02  来源:igfitidea点击:

Multiline regular expression in C#

c#regex

提问by Priyank Bolia

How do I match and replace text using regular expressions in multiline mode?

如何在多行模式下使用正则表达式匹配和替换文本?

I know the RegexOptions.Multilineoption, but what is the best way to specify match all with the new line characters in C#?

我知道RegexOptions.Multiline选项,但是在 C# 中指定 match all 与换行符的最佳方法是什么?

Input:

输入:

<tag name="abc">this
is
a
text</tag>

Output:

输出:

[tag name="abc"]this
is
a
test
[/tag]


Aahh, I found the actual problem. '&' and ';' in Regex are matching text in a single line, while the same need to be escaped in the Regex to work in cases where there are new lines also.

啊哈,我发现了真正的问题。'&' 和 ';' 在正则表达式中匹配一行中的文本,而同样需要在正则表达式中进行转义才能在有新行的情况下工作。

采纳答案by dxh

If you mean there hasto be a newline character for the expression to match, then \nwill do that for you.

如果你的意思是有是为表达式匹配换行符,那么\n就会为你做的。

Otherwise, I think you might have misunderstood the Multiline/Singlelineflags. If you want your expression to match across several lines, you actually want to use RegexOptions.Singleline. What it means is that it treats the entire input string as a single line, thus ignoring newlines. Is this what you're after...?

否则,我认为您可能误解了Multiline/ Singleline标志。如果您希望表达式跨多行匹配,您实际上想要使用RegexOptions.Singleline. 这意味着它将整个输入字符串视为一行,从而忽略换行符。这就是你所追求的......?

Example

例子

Regex rx = new Regex("<tag name=\"(.*?)\">(.*?)</tag>", RegexOptions.Singleline);
String output = rx.Replace("Text <tag name=\"abc\">test\nwith\nnewline</tag> more text...", "[tag name=\"\"][/tag]");

回答by Andomar

Here's a regex to match. It requires the RegexOptions.Singlelineoption, which makes the .match newlines.

这是一个匹配的正则表达式。它需要RegexOptions.Singleline使.匹配换行符的选项。

<(\w+) name="([^"]*)">(.*?)</>

After this regex, the first group contains the tag, the second the tag name, and the third the content between the tags. So replacement string could look like this:

在此正则表达式之后,第一组包含标签,第二组包含标签名称,第三组包含标签之间的内容。所以替换字符串可能如下所示:

[ name=""][/]

In C#, this looks like:

在 C# 中,这看起来像:

newString = Regex.Replace(oldString, 
    @"<(\w+) name=""([^""]*)"">(.*?)</>", 
    "[ name=\"\"][/]", 
    RegexOptions.Singleline);