Why don't all [. \ R \ n] * regex match?

.*

not suitable for

[^\r\n]*

So, if we combine these

[.\r\n]*

why don't we get regexp that matches every line in the world?

+4
source share
1 answer

Like most other special characters in regular expressions, when a .appears with a character class, it represents an alphabetic character .. If you want to combine all the characters, the general method is to use something like this:

[\s\S]*

Or, alternatively, you can use RegexOptions.Singlelineto indicate that it .should match all characters and just use:

.*

For example:

var input = "foo\r\nbar";
var match = Regex.Match(input, ".*", RegexOptions.Singleline);
Assert.AreEqual(input, match.Value);
+7
source

All Articles