Search This Blog

Java: Regular expression to match dot separated numbers

The dot separated values look like below:
1
1.2.3
1.2.3.4.5
The regular expression to match: "^\\d+(\\d*.)*\\d+$"

Java code:
System.out.println(Pattern.matches("^\\d+(\\d*.)*\\d+$", "1")); // true
System.out.println(Pattern.matches("^\\d+(\\d*.)*\\d+$", "1.2.3")); // true
System.out.println(Pattern.matches("^\\d+(\\d*.)*\\d+$", "1.2.a")); // false
System.out.println(Pattern.matches("^\\d+(\\d*.)*\\d+$", "1.2.3.")); // false

No comments:

Post a Comment