9

I'm trying to use some regex in Java and I came across this when debugging my code.

What's the difference between [.] and .?

I was surprised that .at would match "cat" but [.]at wouldn't.

GDanger
  • 1,641
  • 2
  • 22
  • 35
  • 2
    Did you go through any regex tutorial? `.` is a meta-character which matches anything except a newline. Inside a character class, it isn't a meta-character. – Rohit Jain Sep 05 '13 at 15:33
  • Yes, I have been looking at the oracle tutorials. `[abc]` is a character class that matches a, b, or c. So why doesn't `[.]` act the same as `.`? – GDanger Sep 05 '13 at 15:34
  • 1
    ahh, I didn't understand that inside it's own character class it's no longer a meta-character. Thank you. – GDanger Sep 05 '13 at 15:35

3 Answers3

20

[.] matches a dot (.) literally, while . matches any character except newline (\n) (unless you use DOTALL mode).

You can also use \. ("\\." if you use java string literal) to literally match dot.

Bucket
  • 7,415
  • 9
  • 35
  • 45
falsetru
  • 357,413
  • 63
  • 732
  • 636
4

The [ and ] are metacharacters that let you define a character class. Anything enclosed in square brackets is interpreted literally. You can include multiple characters as well:

[.=*&^$] // Matches any single character from the list '.','=','*','&','^','$'

There are two specific things you need to know about the [...] syntax:

  • The ^ symbol at the beginning of the group has a special meaning: it inverts what's matched by the group. For example, [^.] matches any character except a dot .
  • Dash - in between two characters means any code point between the two. For example, [A-Z] matches any single uppercase letter. You can use dash multiple times - for example, [A-Za-z0-9] means "any single upper- or lower-case letter or a digit".

The two constructs above (^ and -) are common to nearly all regex engines; some engines (such as Java's) define additional syntax specific only to these engines.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

regular-expression constructs

. => Any character (may or may not match line terminators)


and to match the dot . use the following

[.] => it will matches a dot
\\. => it will matches a dot

NOTE: The character classes in Java regular expression is defined using the square brackets "[ ]", this subexpression matches a single character from the specified or, set of possible characters.

Example : In string address replaces every "." with "[.]"

public static void main(String[] args) {
    String address = "1.1.1.1";
    System.out.println(address.replaceAll("[.]","[.]"));
}

if anything is missed please add :)