0

What's wrong with the following code?

-(IBAction)numFieldDoneEditing{
        NSPredicate * regexTest = [NSPredicate predicateWithFormat: @"SELF MATCHES '^\\d*\\.\\d{2}$'"];
        NSString *text = [[NSString alloc] initWithFormat: @"%@", numberField.text];
        if ([regexTest evaluateWithObject: text] == YES) {
                //do something
        }
        else{
                //do something else
        }
        [text release];
        [regexTest release];
    }

2 Answers 2

3

For some reason, you have to escape your backslashes twice for the \d sequence in the regex specification:

NSPredicate * regexTest = [NSPredicate predicateWithFormat: @"SELF MATCHES '^\\\\d*\\.\\\\d{2}$'"];

Some other problems with your code:

  • You shouldn't be releasing the regexText object because you're not an owner. It wasn't created with a method named init or copy. See the Memory Management Programming Guide for Cocoa
  • This is more of a style issue, but if you have a boolean variable, don't compare it for equality with YES or NO, it just makes the code harder to understand. Just test it or its inverse directly. For example:

    // Positive test:
    if([regexTest evaluateWithObject: text])
        ; // do stuff
    
    // Negative test
    if(![regexTest evaluateWithObject: text])
        ; // do stuff
    
Sign up to request clarification or add additional context in comments.

3 Comments

About BOOLs: It's more than a style thing. There's no guarantee that a BOOL will only be set to YES or NO. Since BOOL is the same thing as a char, it's entirely possible to say BOOL flag = 42. It's bad style, but it does happen, so your code shouldn't ever test against YES.
Can we replace "for some reason..." with an actual reason?
@OutlawProgrammer well, it is absurd but: first the C compiler chews up every other \, so @"SELF MATCHES '^\\\\d*\\.\\\\d{2}$'" becomes NSString literal SELF MATCHES '^\\d*\.\\d{2}$'. Then NSPredicate parser has a go at the inner literal and it becomes the regular expression ^\d*.\d{2}$. Which makes me think there is error in @Adam Rosenfield's answer - see the decimal point is un-escaped. It should have been @"SELF MATCHES '^\\\\d*\\\\.\\\\d{2}$'" instead. It's PITA, that's what this is.
3

Since you tagged this with iphone and cocoa-touch, it should be worth noting that NSPredicate is not available on the iPhone. It will still work in the simulator, since that uses the desktop frameworks, but it won't work on the iPhone itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.