Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
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
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class GFG {
public static void main(String[] args)
{
String input1 = "abc" ;
String input2 = "1234" ;
String regex = "[+-]?[0-9]+" ;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(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" );
m = p.matcher(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" );
}
}
|
Output
abc 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
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" ;
String regex = "[+-]?[0-9]+(\\.[0-9]+)?([Ee][+-]?[0-9]+)?" ;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(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" );
m = p.matcher(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" );
}
}
|
Output
10e5.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
Last Updated :
13 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...