📌  相关文章
📜  检查给定字符串是否是Java中的有效数字(整数或浮点数) | SET 2(正则表达式方法)

📅  最后修改于: 2022-05-13 01:57:06.812000             🧑  作者: Mango

检查给定字符串是否是Java中的有效数字(整数或浮点数) | SET 2(正则表达式方法)

在 Set 1 中,我们讨论了检查字符串是否为有效数字的一般方法。在这篇文章中,我们将讨论检查数字的正则表达式方法。

例子:

Input : str = "11.5"
Output : true

Input : str = "abc"
Output : false

Input : str = "2e10"
Output : true

Input : 10e5.4
Output : false

检查给定的字符串是否是有效的整数

对于整数:以下是整数的常规定义。

sign -> + | - | epsilon
digit -> 0 | 1 | .... | 9
num -> sign digit digit*

因此,整数的正则表达式之一是

[+-]?[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");
    }
}

输出:

abc is not a valid integer number
1234 is a valid integer number

以下是整数的其他简写正则表达式

[+-]?[0-9]+
[+-]?\d\d*
[+-]?\d+

检查给定的字符串是否是有效的浮点数

对于浮点数:以下是浮点数的常规定义。

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

因此,浮点数的正则表达式之一是

[+-]?[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");
    }
}

输出:

10e5.4 is not a valid float number
2e10 is a valid float number

下面是浮点数的其他简写正则表达式

[+-]?\d+(\.\d+)?([Ee][+-]?\d+)?

相关文章:检查给定字符串是否是Java中的有效数字(整数或浮点数)