How many dots are in a string. (Java)
java, regex
Solution
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
}
Problem
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.