.net – Why does Regex.Escape support number sign and whitespace?
It’s my understanding that Regex.Escape helps produce a regex pattern by automatically converting a pattern that contains regex meta characters to an “escaped” version of that pattern.
So if you want to match only abc.abc you cannot use that as a pattern because the period (or the dot) is a regex meta character that happens to be a wildcard. Regex.Escape conveniently converts it to abc\.abc which can be used as a regex pattern that will not use the dot as a wildcard.
Regex.Escape “escapes” 14 characters…
Escapes a minimal set of characters (, *, +, ?, |, {, [, (,), ^, $,
., #, and white space) by replacing them with their escape codes.
However, there seem to be only 12 characters that do not match themselves. Characters other than these do match themselves.
. $ ^ { [ ( | ) * + ? \
This is the link to the reference that says there are 12 chars that do not match themselves.
The exact wording that makes that claim…
Characters other than those listed in the Character or sequence column
have no special meaning in regular expressions; they match themselves.
The difference is the last 2 characters mentioned in the documentation of Regex.Escape and they are whitespace and number sign. Why does Regex.Escape support more than the 12 characters that do not match themselves?
Read more here: Source link
