public int getNoOfQuestions(){ Pattern p = Pattern.compile("Ans."); Matcher m = p.matcher(text); int count = 0; while (m.find()){ count = count + 1; } return count-3; }