简体   繁体   中英

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).

Edit: I'm using regex with C# language.

Using #C , you can use the RegexOptions.Singleline compiler flag.

Use single-line mode, where ( . ) matches every character (instead of every character except \\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.

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

In perl : Use s modifier.

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

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.

A simple way to surpass this is eighter by using capture groups: (.|\\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. I believe in some languages [^]<\/code> will work as well, but don't know about C#. Basically it says do not match nothing, so it will match anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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