Menu

regex to match only number with n digit



^[0-9]*$



import java.util.regex.Matcher; import java.util.regex.Pattern; final String regex = "^[0-9]*$"; final String string = "12333\n" + "sgd123\n" + "sefgwer46546\n" + "486464se\n" + "486464545656"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println( System.out.println( System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println( System.out.println( System.out.println("Group " + i + ": " + matcher.group(i)); } } 

No comments:

Post a Comment