Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Difficulty Level : Hard
  • Last Updated : 28 Sep, 2020

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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]*




// 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]+)?




//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

This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :