Gowtham
Gowtham

Reputation: 327

Regular Expression for recognizing negative and non-negative values

What is the appropriate regular expression for recognizing negative and non-negative values?

Something like this:

#define DECIMAL_NUMBER_REGEX @"[1-9][0-9]*|0"

Upvotes: 0

Views: 1393

Answers (1)

user529758
user529758

Reputation:

Not sure why this is tagged "iOS", but a regular expression for recognizing non-negative integers may be

\+?0|[1-9][0-9]*

And for negative integers:

\-[1-9][0-9]*

(assuming -0 is treated as non-negative)

If you specifically want to create regular expressions for iOS development, you can use the NSRegularExpression class:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\+?0|[1-9][0-9]*" options:0 error:NULL];

Upvotes: 5

Related Questions