Or you can use capturing groups:
public static void main(final String args[]) throws Exception
{
final String input = "/test/test/test";
final String toCount = "/";
final int n = 2;
System.out.println("Looking for ".concat(n + "x: \"".concat(toCount).concat("\"")));
System.out.println("RESULT for \"".concat(input).concat("\": ") + filterChars(input, toCount, n));
}
public static boolean filterChars(final String s, final String toCount, final int n)
{
// Oh boy, this is the best... string concatenation to create regex patterns... what could go wrong?
// Please... think of the children
final Pattern p = Pattern.compile("(" + toCount + ")");
final Matcher m = p.matcher(s);
int count = 0;
while (m.find())
{
count++;
}
return (n <= count);
}