50

What is the regular expression for . and .. ?

if(key.matches(".")) {

do something 

}

The matches accepts String which asks for regular expression. Now i need to remove all DOT's inside my MAP.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
John
  • 1,191
  • 3
  • 19
  • 29

5 Answers5

95

. matches any character so needs escaping i.e. \., or \\. within a Java string (because \ itself has special meaning within Java strings.)

You can then use \.\. or \.{2} to match exactly 2 dots.

mikej
  • 65,295
  • 17
  • 152
  • 131
  • 1
    \. does not work with above code in key.matches – John Oct 05 '10 at 10:03
  • 8
    As mentioned, `\.` is the regular expression but to construct a String for this expression in Java you will need `\\.` because \ is used within Java strings for things like \n, \t etc. so \\ is needed for a literal \ within a String. Also, note that `String.matches` requires the regexp to match the **entire** String. If you want to do a substring search you will need to use `Matcher.find` – mikej Oct 05 '10 at 14:32
  • 3
    Also, can you clarify the requirements a bit. If you just need to check if the key contains a dot then you can use `key.contains` and don't really need a regexp at all. – mikej Oct 05 '10 at 14:33
  • This need very high level understanding, because at initial level its very difficult to fond such important thing. – Pushkar Nov 17 '14 at 12:05
  • 1
    You can use ``Pattern#quote(String)`` to escape reserved RegEx characters – Binkan Salaryman Oct 16 '15 at 15:00
  • 1
    Javascript also need to escape slashed ('\\.') – Teetrinker Nov 11 '15 at 14:26
16

...

[.]{1}

or

[.]{2}

?

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
8

[+*?.] Most special characters have no meaning inside the square brackets. This expression matches any of +, *, ? or the dot.

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
3

Use String.Replace() if you just want to replace the dots from string. Alternative would be to use Pattern-Matcher with StringBuilder, this gives you more flexibility as you can find groups that are between dots. If using the latter, i would recommend that you ignore empty entries with "\\.+".

public static int count(String str, String regex) {
    int i = 0;
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(str);
    while (m.find()) {
        m.group();
        i++;
    }
    return i;
}

public static void main(String[] args) {
    int i = 0, j = 0, k = 0;
    String str = "-.-..-...-.-.--..-k....k...k..k.k-.-";

    // this will just remove dots
    System.out.println(str.replaceAll("\\.", ""));
    // this will just remove sequences of ".." dots
    System.out.println(str.replaceAll("\\.{2}", ""));
    // this will just remove sequences of dots, and gets
    // multiple of dots as 1
    System.out.println(str.replaceAll("\\.+", ""));

    /* for this to be more obvious, consider following */
    System.out.println(count(str, "\\."));
    System.out.println(count(str, "\\.{2}"));
    System.out.println(count(str, "\\.+"));
}

The output will be:

--------kkkkk--
-.--.-.-.---kk.kk.k-.-
--------kkkkk--
21
7
11
Margus
  • 19,694
  • 14
  • 55
  • 103
1

You should use contains not matches

if(nom.contains("."))
    System.out.println("OK");
else
    System.out.println("Bad");
fperez
  • 482
  • 1
  • 5
  • 17