Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
Last Updated :
13 Jul, 2022
In Set 1, we have discussed general approach to check whether a string is a valid number or not. In this post, we will discuss regular expression approach to check for a number.
Examples:
Input : str = "11.5"
Output : true
Input : str = "abc"
Output : false
Input : str = "2e10"
Output : true
Input : 10e5.4
Output : false
Check if a given string is a valid Integer
For integer number : Below is the regular definition for an integer number.
sign -> + | - | epsilon
digit -> 0 | 1 | .... | 9
num -> sign digit digit*
Hence one of the regular expression for an integer number is
[+-]?[0-9][0-9]*
Implementation:
Java
// Java program to check whether given string
// is a valid integer number using regex
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
public static void main(String[] args)
{
String input1 = "abc";
String input2 = "1234";
// regular expression for an integer number
String regex = "[+-]?[0-9]+";
// compiling regex
Pattern p = Pattern.compile(regex);
// Creates a matcher that will match input1 against
// regex
Matcher m = p.matcher(input1);
// If match found and equal to input1
if (m.find() && m.group().equals(input1))
System.out.println(
input1 + " is a valid integer number");
else
System.out.println(
input1 + " is not a valid integer number");
// Creates a matcher that will match input2 against
// regex
m = p.matcher(input2);
// If match found and equal to input2
if (m.find() && m.group().equals(input2))
System.out.println(
input2 + " is a valid integer number");
else
System.out.println(
input2 + " is not a valid integer number");
}
}
Outputabc is not a valid integer number
1234 is a valid integer number
Below are other short-hands regular expression for an integer number
[+-]?[0-9]+
[+-]?\d\d*
[+-]?\d+
Check if a given string is a valid floating point number
For floating point number : Below is the regular definition for a floating point number.
sign -> + | - | epsilon
digit -> 0 | 1 | .... | 9
digits -> digit digit*
optional_fraction -> . digits | epsilon
optional_exponent -> ((E | e) (+ | - | epsilon) digits) | epsilon
num -> sign digits optional_fraction optional_exponent
Hence one of the regular expression for a floating number is
[+-]?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?
Implementation:
Java
//Java program to check whether given string
// is a valid floating point number using regex
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG
{
public static void main (String[] args)
{
String input1 = "10e5.4";
String input2 = "2e10";
// regular expression for a floating point number
String regex = "[+-]?[0-9]+(\\.[0-9]+)?([Ee][+-]?[0-9]+)?";
// compiling regex
Pattern p = Pattern.compile(regex);
// Creates a matcher that will match input1 against regex
Matcher m = p.matcher(input1);
// If match found and equal to input1
if(m.find() && m.group().equals(input1))
System.out.println(input1 + " is a valid float number");
else
System.out.println(input1 + " is not a valid float number");
// Creates a matcher that will match input2 against regex
m = p.matcher(input2);
// If match found and equal to input2
if(m.find() && m.group().equals(input2))
System.out.println(input2 + " is a valid float number");
else
System.out.println(input2 + " is not a valid float number");
}
}
Output10e5.4 is not a valid float number
2e10 is a valid float number
Below is other short-hand regular expression for a float number
[+-]?\d+(\.\d+)?([Ee][+-]?\d+)?
Related Article : Check if a given string is a valid number (Integer or Floating Point) in Java
Similar Reads
Check if a given string is a valid number (Integer or Floating Point) in Java In the article Check if a given string is a valid number, we have discussed general approach to check whether a string is a valid number or not. In Java we can use Wrapper classes parse() methods along with try-catch blocks to check for a number. For integer number Integer class provides a static me
4 min read
Check if a given string is a valid number (Integer or Floating Point) | SET 1(Basic approach) Validate if a given string is numeric. Examples: Input : str = "11.5" Output : true Input : str = "abc" Output : false Input : str = "2e10" Output : true Input : 10e5.4 Output : false The following cases need to be handled in the code. Ignore the leading and trailing white spaces.Ignore the '+', '-'
11 min read
How to check Aadhaar number is valid or not using Regular Expression Given string str, the task is to check whether the given string is a valid Aadhaar number or not by using Regular Expression. The valid Aadhaar number must satisfy the following conditions: It should have 12 digits.It should not start with 0 and 1.It should not contain any alphabet and special chara
5 min read
Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach) Given a string of 0 and 1, we need to check that the given string is valid or not. The given string is valid when there is no zero is present in between 1's. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not. Examples: Input : 100 Output : VALID Input : 111000
3 min read
How to validate Indian Passport number using Regular Expression Given a string str of alphanumeric characters, the task is to check whether the given string is a valid passport number or not by using Regular Expression. A valid passport number in India must satisfy the following conditions: It should be eight characters long.The first character should be an uppe
5 min read
How to validate Indian driving license number using Regular Expression Given string str, the task is to check whether the given string is a valid Indian driving license number or not by using Regular Expression.The valid Indian driving license number must satisfy the following conditions: It should be 16 characters long (including space or hyphen (-)).The driving licen
7 min read
Validate Corporate Identification Number (CIN) using Regular Expression Given some Corporate Identification Number, the task is to check if they are valid or not using regular expressions. Rules for the valid CIN are: CIN is a 21 digits alpha-numeric code.It starts with either alphabet letter U or L.Next five characters are reserved for digits (0-9).Next two places are
6 min read
Program to construct a DFA to check if a given integer is unsigned or not Given a string S that represents an integer, the task is to check if the given string S represents an unsigned integer or not by constructing the DFA. If the given string represents an unsigned integer, then print "Unsigned integer". Otherwise, print "Not an unsigned integer". Examples: Input: S = "
10 min read
Check whether given floating point number is even or odd Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number
6 min read
How to validate pin code of India using Regular Expression Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression. The valid pin code of India must satisfy the following conditions. It can be only six digits.It should not start with zero.First digit of the pin cod
6 min read