14

I am looking for a regular expression to match the following -100..100:0.01. The meaning of this expression is that the value can increment by 0.01 and should be in the range -100 to 100.

Any help ?

yonel
  • 7,855
  • 2
  • 44
  • 51
Cannon
  • 2,725
  • 10
  • 45
  • 86
  • 1
    What does 'want to work out this thing in regular expression' mean? – Mauritz Hansen Mar 29 '11 at 20:01
  • want to get a regular expression, which I can evaluate in my iOS app. – Cannon Mar 29 '11 at 20:15
  • Is `-100..100:0.01` a literal representation, or can you give some examples. Otherwise its a waste of time. –  Mar 29 '11 at 20:38
  • it means the value range is -100 to 100, inbetween values can be in increment of .01, i.e. only with 2 digits after decimal. So range will be like..[ -100.00, -99.99,-99.98,...,99.99,100.00 ]. Hope this gives clear idea. – Cannon Mar 29 '11 at 20:56

4 Answers4

30

You could use NSRegularExpression instead. It does support \b, btw, though you have to escape it in the string:

NSString *regex = @"\\b-?1?[0-9]{2}(\\.[0-9]{1,2})?\\b";

Though, I think \\W would be a better idea, since \\b messes up detecting the negative sign on the number.

A hopefully better example:

NSString *string = <...your source string...>;
NSError  *error  = NULL;

NSRegularExpression *regex = [NSRegularExpression 
  regularExpressionWithPattern:@"\\W-?1?[0-9]{2}(\\.[0-9]{1,2})?\\W"
                       options:0
                         error:&error];

NSRange range   = [regex rangeOfFirstMatchInString:string
                              options:0 
                              range:NSMakeRange(0, [string length])];
NSString *result = [string substringWithRange:range];

I hope this helps. :)

EDIT: fixed based on the below comment.

liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
SynTruth
  • 401
  • 4
  • 5
10
(\b|-)(100(\.0+)?|[1-9]?[0-9](\.[0-9]{1,2})?\b

Explanation:

(\b|-)      # word boundary or -
(           # Either match
 100        #  100
 (\.0+)?    #  optionally followed by .00....
|           # or match
 [1-9]?     #  optional "tens" digit
 [0-9]      #  required "ones" digit
 (          #  Try to match
  \.        #   a dot
  [0-9]{1,2}#   followed by one or two digits
 )?         #   all of this optionally
)           # End of alternation
\b          # Match a word boundary (make sure the number stops here).
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
2

Why do you want to use a regular expression? Why not just do something like (in pseudocode):

is number between -100 and 100?
  yes:
    multiply number by 100
    is number an integer?
      yes: you win!
      no:  you don't win!
  no:
    you don't win!
CanSpice
  • 34,814
  • 10
  • 72
  • 86
1
          if(val>= -100 && val <= 100)
    {
        NSString* valregex = @"^[+|-]*[0-9]*.[0-9]{1,2}"; 
        NSPredicate* valtest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", valregex]; 
        ret = [valtest evaluateWithObject:txtLastname.text];
        if (!ret)
        {
            [alert setMessage:NSLocalizedString(@"More than 2 decimals", @"")];
            [alert show];       
        }
    }

works fine.. Thnx for the efforts guys !

Cannon
  • 2,725
  • 10
  • 45
  • 86