0gravity
0gravity

Reputation: 2762

How many dots are in a string. (Java)

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.

Upvotes: 5

Views: 15464

Answers (7)

Giedrius Šlikas
Giedrius Šlikas

Reputation: 1201

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;
    }

Upvotes: 1

Nate Bosch
Nate Bosch

Reputation: 10915

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

boolean multipleDots = StringUtils.countMatches( myString, "." ) > 1;

Upvotes: 6

dosakiller
dosakiller

Reputation: 79

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}?")

Upvotes: 1

Aleksander Blomsk&#248;ld
Aleksander Blomsk&#248;ld

Reputation: 18542

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
}

Upvotes: 6

MadProgrammer
MadProgrammer

Reputation: 347194

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

if (myString.indexOf(".") != myString.lastIndexOf(".")) {
    // More then one dot...
}

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1500165

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(".."))

Upvotes: 8

Alex
Alex

Reputation: 25613

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}?")

Upvotes: 1

Related Questions