1

I'm having issue assigning regex value to a string. Issue that I have is I have \d in the string value and objective-c complaining!

Here is my code

I'm assigning regex value here.

- (BOOL) validateUSZipCode: (NSString *) candidate {
   NSString *regex = @"^\d{5}(-\d{4})?$"; 
   NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 

   return [test evaluateWithObject:candidate];
}

Someone help me please!

2
  • 1
    What do you mean by "complaining?" Commented Oct 20, 2011 at 16:52
  • it warns me: Unknown escape sequence '\d' Commented Oct 20, 2011 at 16:55

1 Answer 1

2

You need to quote backslashes to include them in a constant NSString.

NSString *regex = @"^\\d{5}(-\\d{4})?$"; 
Sign up to request clarification or add additional context in comments.

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.