Objective-C的Regex解决方案 [英] Regex solution for Objective-C

查看:168
本文介绍了Objective-C的Regex解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中使用正则表达式的最佳方法是什么?



似乎有一些开源项目提供 regex 支持,任何人都可以推荐一个?



同时我看看 NSPredicate 任何人都建议任何正则表达式示例?



背景:我想使用regex主要用于验证,IP,电子邮件地址,内部ID等。

解决方案

请阅读 full在 http://cocoawithlove.com 上发布。



在Cocoa中,NSPredicate在
中的工作方式与
SQL的WHERE子句相同。 NSPredicate
被引入到iPhone的主要原因是
NSPredicate在
中实现相同的角色核心数据,WHERE子句在SQL中满足
- 允许持久化存储
以获取满足特定
条件的对象。




  /给定一个NSDictionary创建使用上述方法命名为row... 
NSPredicate * johnSmithPredicate =
[NSPredicate predicateWithFormat:@firstname ='John'AND lastname ='Smith'
BOOL rowMatchesPredicate = [johnSmithPredicate evaluateWithObject:row];

验证电子邮件地址



中的LIKE比较运算符NSPredicate
(NSLikePredicateOperatorType)是
,通常用作
的方便方法NSString匹配一个
正则表达式。它是优势
超过完整的图书馆更大的
选项和替换能力是
它已经在可可 - 没有
库,没有链接,没有麻烦。


您可以做一个简单的测试:

  NSPredicate * regExPredicate = 
[NSPredicate predicateWithFormat:@SELF MATCHES%@,regularExpressionString];
BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject:myString];




唯一的问题是:
什么是可以使用
的正则表达式来验证NSString
是否包含语法有效的电子邮件
地址?




  NSString * emailRegEx = 
@(?:[a-z0-9!#$%\\&'* + / =?\\\ \\ ^ _` {|}〜 - ] +(?: \\。[a-z0-9!#$%\\&'* + / =?\\ ^ _`{| }
@〜 - ] +)* | \(?:[\\x01-\\x08\\x0b\\x0c\\x0e- \\\x1f\\x21\\x23 -\\x5b\\x5d -\\
@x7f] | \\\\ [\\ \\\x01-\\x09\\x0b\\x0c\\x0e -\\x7f])* \)@(?:(?:[a-z0- 9](α:[a-
@z0-9 - ] * [a-z0-9])?\\。)+ [a-z0-9] z0-9 - ] * [a-z0-9])?| \\ [(?:?:25 [0-5
@] | 2 [0-4] [0- 9] | [01]?[0-9] [0-9]?)\\。){3}(?:25 [0-5] | 2 [0-4] [01]?[0-
@9] [0-9]?| [a-z0-9 - ] * [a-z0-9] :(?:[\\ x01- \\x08\\x0b\\x0c\\x0e -\\x1f\\x21
@ - \\x5a\\x53- \\x7f] | \\\\ [\\x01 -\\x09\\x0b\\x0c\\x0e -\\x7f]) +)\\]);


What is the best method for using Regular Expressions within Objective-C?

There seems to be some open source project that provide regex support, can any one recommend one?

Also I looked at NSPredicate, can anyone suggest any regex examples?

Background: I want use regex mainly for validation, IP's, email addresses, internal ID's etc

解决方案

Please read the full post over at http://cocoawithlove.com.

In Cocoa, NSPredicate works in much the same way as the "WHERE" clause of SQL. The main reason that NSPredicate is being brought to the iPhone is that NSPredicate fulfils the same role in Core Data that "WHERE" clauses fulfil in SQL — to allow the persistent store to fetch objects that satisfy specific criteria.

// given an NSDictionary created used the above method named "row"...
NSPredicate *johnSmithPredicate =
    [NSPredicate predicateWithFormat:@"firstname = 'John' AND lastname = 'Smith'"];
BOOL rowMatchesPredicate = [johnSmithPredicate evaluateWithObject:row];

Verifying an email address

The "LIKE" comparison operator in NSPredicate (NSLikePredicateOperatorType) is commonly used as a convenient means of testing if an NSString matches a Regular Expression. It's advantage over full libraries with greater options and replacement capability is that it is already in Cocoa — no libraries, no linkage, no hassle.

You can do a simple test:

NSPredicate *regExPredicate =
    [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regularExpressionString];
BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject:myString];

The only question that remains is: what is a regular expression that can be used to verify that an NSString contains a syntactically valid email address?

NSString *emailRegEx =
    @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
    @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
    @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";

这篇关于Objective-C的Regex解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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