Do not need to escape the "." in a field [...] in REGEX?

8

I was seeing some regular expressions here on the network, and I noticed that in many, in the character field, . was not escaped \. . Thus: [a-zA-Z0-9.] . It does not need the \ character before, does it?

    
asked by anonymous 28.11.2015 / 03:59

2 answers

9

No, because it would not make sense to allow "any character" inside brackets. Consider the given example. If the point inside the bracket is special, the expression:

[a-zA-Z0-9.]

would be equivalent to simply:

.

(i.e. both match "any one character")

Since there is no sense to use this way, the . within the bracket is considered a point, and therefore does not have to be "escaped."

    
28.11.2015 / 05:29
0

Have different meanings . can be any character, since \. is understood as the dot character.
But you should note the expression context , in which case it is concatenating the ranges from az, AZ, 0-9 and also accepts the dot character.

    
28.11.2015 / 04:47