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

10,988

Solution 1

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");

Solution 2

I am not familiar with C#, but I am sure regex works the identically everywhere.

.* Matches any character, right, excepts for \n. A simple way to surpass this is eighter by using capture groups: (.|\n)*; (.|\n|\r)*, or you can surpass this limitation by using [\s\S], where \s is any whitespace, and \S is any non white space. I believe in some languages [^] will work as well, but don't know about C#. Basically it says do not match nothing, so it will match anything.

Share:
10,988

Related videos on Youtube

Bình Nguyên
Author by

Bình Nguyên

Updated on September 16, 2022

Comments

  • Bình Nguyên
    Bình Nguyên over 1 year

    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.

    • Jonathan Leffler
      Jonathan Leffler almost 10 years
      Which dialect of regex — or which host language?
    • Jonathan Leffler
      Jonathan Leffler almost 10 years
      Yes; there are lots of differences between the regexes in C# and 'the original regex' — but almost nothing except the ed editor uses the original regex. Oh, and plain vanilla grep. When asking about regexes, it is important to specify the host language, because what works in C# may not work in PHP, Perl, Tcl/Tk, Python, Ruby, C, C++, grep, grep -E, sed, ed, Java, etc., and vice versa.