5

So, I am working on a program in which I need to check if a string has more than one dot. I went and looked at the documentation for regex and found this:

X{n,m}?  X, at least n but not more than m times

Following that logic I tried this:

mString = "He9.lo,"

if (myString.matches(".{1,2}?")){

    System.out.print("it works");
}

But that does not works (It does not gives any error, it just wont do what is inside the if). I think the problem is that is thinking that the dot means something else. I also looked at this question: regex but is for php and I don't know how to apply that to java.

EDIT: Basically I want to know if a string contains more than one dot, for example:

He......lo (True, contains more than one), hel.llo (False, contains one), ..hell.o (True Contains more than one).

Any help would be appreciated.

Community
  • 1
  • 1
0gravity
  • 2,682
  • 4
  • 24
  • 33
  • If you accept non regex answers: Possible duplicate of http://stackoverflow.com/questions/767759/occurences-of-substring-in-a-string – Kai Mattern Aug 22 '12 at 22:39

7 Answers7

8

A dot in a regular expression means "any character". So if you want actual dots you need to escape it with a backslash - and then you need to escape the backslash within Java itself...

if (myString.matches("\\.{1,2}?")){

Next you need to understand that matches tries to match the whole text - it's not just trying to find the pattern within the string.

It's not entirely clear what your requirements are - is it just "more than one dot together" or "more than one dot anywhere in the text"? If it's the latter, this should do it:

if (myString.matches(".*\\..*\\..*")) {

... in other words, anything, then a dot, then anything, then a dot, then anything - where the "anything" can be "nothing". So this will match "He..llo" and "H.e.llo", but not "He.llo".

Hopefully that's what you want. If you literally just want "it must have .. in it somewhere" then it's easier:

if (myString.contains(".."))
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the answer, but I tried that and it does not do what's inside the if statement. Am I missing something? – 0gravity Aug 22 '12 at 22:39
  • Yes, but according to the documentation it should look if the dot appears 1, but no more than 2 times in the whole string. Is that correct? – 0gravity Aug 22 '12 at 22:43
  • Basically what I want to see if there is more than one dot in the whole string. – 0gravity Aug 22 '12 at 22:52
6

As mentioned in the other answers, your regex contains a bug. You could either fix that (escape the dot), or use String.contains instead:

if (myString.contains("..")){
    System.out.print("it works");
}

This will match for two consecutive dots. If you're also looking for two dots anywhere in the string (like in the string "He.llo.") you could do

if(myString.indexOf('.', myString.indexOf('.') + 1) != -1) {
    System.out.print("it works"); //there are two or more dots in this string
}
Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
6

StringUtils.countMatches makes it more immediately obvious what is happening.

boolean multipleDots = StringUtils.countMatches( myString, "." ) > 1;
Nate Bosch
  • 10,145
  • 2
  • 26
  • 22
4

I think the other answers are better, however, just for a different perspective

if (myString.indexOf(".") != myString.lastIndexOf(".")) {
    // More then one dot...
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

You can use [] or \ to escape special characters, which the dot character . is, meaning any character.

For example using brackets to escape it: myString.matches("[.]{1,2}?")

Alex
  • 25,147
  • 6
  • 59
  • 55
1

You need to use escape sequence characters for those characters which are already part of the regex metacharacters set

\ | ( ) [ { ^ $ * + ? . < >

Since the "." character is also part of the meta characters set, You need to use "\." to match the string pattern with multiple "." characters.

in this case myString.matches("\\.{1,2}?")

dosakiller
  • 79
  • 5
1

this solution works perfectly for me if I want to check does the string has 2 dots. In other words it's a validation for URL

private boolean isValid(String urlString) {
        try {
            String extensionValidator;
            extensionValidator = urlString.substring(urlString.lastIndexOf('.') + 1);
            URL url = new URL(urlString);

            if (urlString.indexOf('.', urlString.indexOf('.') + 1) != -1) {
                if (extensionValidator.length() >= 2 && urlString.contains(".")) {
                    return URLUtil.isValidUrl(urlString) && Patterns.WEB_URL.matcher(urlString).matches();
                }
            }

        } catch (MalformedURLException e) {

        }

        return false;
    }
Giedrius Šlikas
  • 1,073
  • 12
  • 12