Java - Write code to check if a string is Integer using regex

Requirements

Write code to check if a string is Integer

Demo

import java.util.regex.Pattern;

public class Main {
  public static void main(String[] argv) {
    String s = "123";
    System.out.println(isInteger(s));
  }//from  ww w.  j a  v a  2  s  .  co m

  private static final String POSITIVE_INTEGER_REGEX = "[0-9]+";
  private static final String INTEGER_REGEX = "-?" + POSITIVE_INTEGER_REGEX;
  private static final Pattern INTEGER_PATTERN = Pattern.compile(INTEGER_REGEX);

  public static final boolean isInteger(String s) {
    return INTEGER_PATTERN.matcher(s).matches();
  }
}

Related Exercise