如何检查字符串是否与Objective-C中的正则表达式匹配? [英] How to check if string matches a regular expression in objective-c?

查看:75
本文介绍了如何检查字符串是否与Objective-C中的正则表达式匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为可可中不支持常规扩展名,所以我找到了 RegexKitLite 非常有用. 但是所有示例都提取匹配的字符串.

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.

我该怎么办?

推荐答案

我为此目的使用了NSPredicate:

I've used NSPredicate for that purpose:

NSString *someRegexp = ...; 
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", someRegexp]; 

if ([myTest evaluateWithObject: testString]){
//Matches
}

这篇关于如何检查字符串是否与Objective-C中的正则表达式匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆