regexp dot with square brackets or not
What is the exact difference between .*
and [.]*
?
I have tried to put these inside parentheses, for back-reference, and saw the results are not the same although I don't understand why.
The .
is supposed to match any single character.
So I guess whether it's inside square brackets or not should not be important with the *
(match zero or more) operator. But it is. Why?
Solution 1:
In .*
, a dot is a special character matching any character but a newline (it will match all characters if you specify a DOTALL modifier). In a character class ([.]
), a dot loses its special meaning and starts matching a literal dot.
It is a universal behavior across all regex flavors.
Solution 2:
-
.
matches any character, apart from newline. -
\.
only matches a literal"."
. -
[.]
is equivalent to[\.]
or\.
This is just for convenience - because you almost certainty don't want it to match "any character", in the context of a character group.
Bonus -- If you use my ruby gem, you can easily experiment with stuff like this:
/./.examples # => ["a", "b", "c", "d", "e"]
/\./.examples # => ["."]
/[.]/.examples # => ["."]