I'm trying to find every occurrence of ".-." in this String "-.-.-.-", so I would expect an output of 2, instead I just get 1.
If i search on this String "-.-.--.--.-.--", I expect 2 and I do get 2. So it appears to me that the search doesn't recognize individual ".-." when they're grouped next to each other. How do I fix that?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test {
public static void find() {
int count = 0;
Pattern p = Pattern.compile("\\.-\\.");
Matcher m = p.matcher("-.-.-.-");
while (m.find()) {
count++;
}
System.out.println(count);
}
public static void main(String[] args) {
find();
}
}