Search code examples
objective-cregexkitlite

How to check if string matches a regular expression in objective-c?


since regular exressions are not supported in Cocoa I find RegexKitLite very usefull. But all examples extract matching strings.

I just want to test if a string matches a regular expression and get a Yes or No.

How can I do that?


Solution

  • I've used NSPredicate for that purpose:

    NSString *someRegexp = ...; 
    NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", someRegexp]; 
    
    if ([myTest evaluateWithObject: testString]){
    //Matches
    }