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

前端 未结 4 817
庸人自扰
庸人自扰 2021-01-30 21:56

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

4条回答
  •  别那么骄傲
    2021-01-30 22:27

    I've used NSPredicate for that purpose:

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

提交回复
热议问题