Tim
Tim

Reputation: 1074

JAVA - Regex Issue

Just testing out some special regex characters and just came across some behaviour I could not explain. An un escaped full-stop stands for any character, therefore (for example) matching 3 to "." returns true, however, when I match 3 to "[.]", it returns false. What is the cause of this?

Upvotes: 1

Views: 57

Answers (2)

dash1e
dash1e

Reputation: 7807

Inside the squares "." means full-stop and not any char.

So the regex "[.]" match "." and not "3".

Upvotes: 1

cHao
cHao

Reputation: 86595

Like most other metacharacters, . loses its special meaning in a character class (the []). There, it just means "a dot".

Upvotes: 4

Related Questions