A colleague from C + + asked me to help write a regular expression: Verify that a value is a number. I simply wrote one in Java code.
public class Test {public static Boolean CheckNumber (double value) {String str = string.valueof (value); String regex = "^" (-?[ 1-9]\\d*\\.? \\d*) | (-?0\\.\\d*[1-9]) | (-? [0]) | (-?
[0]\\.\\d*] $ ";
return str.matches (regex);
public static Boolean checknumber (int value) {String str = string.valueof (value); String regex = "^" (-?[ 1-9]\\d*\\.? \\d*) | (-?0\\.\\d*[1-9]) | (-? [0]) | (-?
[0]\\.\\d*] $ ";
return str.matches (regex); public static Boolean CheckNumber (String value) {string regex = ' ^ (-?[ 1-9]\\d*\\.? \\d*) | (-?0\\.\\d*[1-9]) | (-? [0]) | (-?
[0]\\.\\d*] $ ";
return value.matches (regex); public static void Main (string[] args) {System.out.println (CheckNumber (0));/True System.out.println (Checknumbe R ( -0));//True System.out.println (CheckNumber);/True System.out.println (CheckNumber ( -23));/True SYSTEM.OUT.P Rintln (CheckNumber (0.0));/True System.out.println (CheckNumber ( -0.0));/True System.out.println (CheckNumber ( 23.01));/True System.out.println (checknUmber ( -23.01))//True System.out.println (CheckNumber (" -10.01.01"));/False System.out.println (CheckNumber ("A110" );/False}}
If there is a mistake, please advise, there is better, more please enlighten ah, thank you.