Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesnt [.\r\n]* regex match everything?

Tags:

c#

.net

regex

.*

is short for

[^\r\n]*

So if we combine these

[.\r\n]*

why dont we get regexp that matches every string in the world?

like image 539
foxneZz Avatar asked Mar 21 '23 15:03

foxneZz


1 Answers

Like most other special characters in regular expressions, when a . appears withing a character class it represents a literal . character. If you want to match all characters a common technique is to use something like this:

[\s\S]*

Or alternatively you can use RegexOptions.Singleline to specify that . 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);
like image 168
p.s.w.g Avatar answered Apr 06 '23 09:04

p.s.w.g