java中正和负十进制值的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2072222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:16:14  来源:igfitidea点击:

Regular expression for positive and a negative decimal value in java

javaregex

提问by Abhi

Regular expression for negative and positive decimal value so that can be matched with string using pattern and matcher for the correct implementation can any one will provide me with that

负和正十进制值的正则表达式,以便可以使用模式和匹配器与字符串匹配以进行正确的实现,任何人都可以为我提供

采纳答案by FreddyB

(\+|-)?([0-9]+(\.[0-9]+))

 

 

回答by Rubens Farias

Try this:

尝试这个:

[+-]?\d+\.\d+

回答by RonY

Try This! I've also used this way

尝试这个!我也用过这种方式

"^-?[0-9]{1,12}(?:\.[0-9]{1,4})?$"

Rules:

规则:

ex: ^-?[0-9]{1,12}(?:\.[0-9]{1,4})?$
^           # Start of string
[0-9]{1,12} # Match 1-12 digits (i. e. 0-999999999999)
(?:         # Try to match...
\.          # a decimal point
[0-9]{1,4}  # followed by one to three digits (i. e. 0-9999)
)?          # ...optionally
$            # End of string

回答by Abdurrahman Mubeen Ali

+ (BOOL)isStringADecimalNumber:(NSString *)string
{
    NSString *regex = @"([+]|-)?(([0-9]+[.]?[0-9]*)|([0-9]*[.]?[0-9]+))";

    NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    BOOL stringIsADecimalNumber = [test evaluateWithObject:string];

    return stringIsADecimalNumber;
}

Highlighted below, are the numbers that return TRUEfor this regex: enter image description hereenter image description here

下面突出显示的是TRUE此正则表达式返回的数字: 在此处输入图片说明在此处输入图片说明