简体   繁体   English

正则表达式中的任何字符(包括换行符)模式是什么?

[英]What is any character (including new line) pattern in regex?

Does regex have a pattern that match any characters including new line in regex?正则表达式是否具有匹配任何字符的模式,包括正则表达式中的换行符? The dot pattern match any characters but isn't including new line, (currently, I'm using [^~] because the ~ character is rarely use). dot模式匹配任何字符,但不包括换行符,(目前,我使用[^~]因为~字符很少使用)。

Edit: I'm using regex with C# language.编辑:我正在使用带有C#语言的regex

Using #C , you can use the RegexOptions.Singleline compiler flag.使用#C ,您可以使用RegexOptions.Singleline编译器标志。

Use single-line mode, where ( . ) matches every character (instead of every character except \\n )使用单行模式,其中 ( . ) 匹配每个字符(而不是除\\n之外的每个字符)

And instead of the RegexOptions.Singleline compiler flag, you can get the same effect by placing an inline modifier at the very beginning of your regular expression.而不是 RegexOptions.Singleline 编译器标志,您可以通过在正则表达式的开头放置一个内联修饰符来获得相同的效果。

Regex.Match(input, @"(?s)foo.*bar");

In perl : Use s modifier. perl中 :使用s修饰符。

Treat string as single line. 将字符串视为单行。 That is, change "." 也就是说,改变“。” to match any character whatsoever, even a newline, which normally it would not match. 匹配任何字符,甚至是换行符,通常它不匹配。

http://perldoc.perl.org/perlre.html http://perldoc.perl.org/perlre.html

Example: 例:

#!/usr/bin/env perl

$_ = "Word1Word2Word3Word4\nEuropeWorld";

if ( m/Word4.Europe/s ) {
    print "yes\n";
    }

I am not familiar with C#, but I am sure regex works the identically everywhere.我不熟悉 C#,但我确信正则表达式在任何地方都一样。

.*<\/code> Matches any character, right, excepts for \\n<\/code> . .*<\/code>匹配任何字符,对,除了\\n<\/code> 。 A simple way to surpass this is eighter by using capture groups: (.|\\n)*<\/code> ;一个简单的方法是通过使用捕获组来超越它: (.|\\n)*<\/code> ; (.|\\n|\\r)*<\/code> , or you can surpass this limitation by using [\\s\\S]<\/code> , where \\s<\/code> is any whitespace, and \\S<\/code> is any non white space. (.|\\n|\\r)*<\/code> ,或者您可以使用[\\s\\S]<\/code>来超越此限制,其中\\s<\/code>是任何空格,而\\S<\/code>是任何非空格。 I believe in some languages [^]<\/code> will work as well, but don't know about C#.我相信某些语言[^]<\/code>也可以,但不了解 C#。 Basically it says do not match nothing, so it will match anything.基本上它说不匹配任何东西,所以它会匹配任何东西。

"

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

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