Regular expression for positive and a negative decimal value in java
Regular expression for negative and positive decimal value so that can be matched with string using pattern and matcher for the correct imple开发者_Python百科mentation can any one will provide me with that
(\+|-)?([0-9]+(\.[0-9]+))
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
Try this:
[+-]?\d+\.\d+
+ (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 TRUE
for this regex:
精彩评论