Open In App
Related Articles

Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)

Improve Article
Improve
Save Article
Save
Like Article
Like

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");
    }
}


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




//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");
    }
}


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 

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 13 Jul, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials