package ihic; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { String ss = "[RIM-222] [RIM-22222]"; Pattern pat = Pattern.compile("\\[RIM\\-\\d*\\]"); Matcher mat = pat.matcher(ss); int i = 0; while (mat.find()){ i++; } if (i == 0){ i = 1; } System.out.println(i); } }