"^[1-9]\\d*$"
is used to match the positive integer.
App.java
package com.sample.app; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class App { private static final String regEx = "^[1-9]\\d*$"; public static boolean isPositiveNumber(int number) { Pattern pattern = Pattern.compile(regEx); Matcher matcher = pattern.matcher(number + ""); return matcher.matches(); } public static void main(String args[]) throws IOException { System.out.println("Is 123 positive number : " + isPositiveNumber(123)); System.out.println("Is -123 positive number : " + isPositiveNumber(-123)); System.out.println("Is 0 positive number : " + isPositiveNumber(0)); System.out.println("Is 54 positive number : " + isPositiveNumber(54)); } }
Output
Is 123
positive number : true
Is -123
positive number : false
Is 0
positive number : false
Is 54
positive number : true
You may
like
No comments:
Post a Comment