views:

1924

answers:

6

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

+1  A: 

try this: http://regexkit.sourceforge.net/

Mr. Vile
I haven't tried it but this seems to be exactly the right thing. (Note the requirement to include a copyright notice in distributed programs using the framework.)
Andrew J. Brehm
+4  A: 
Marcus S. Zarra
http://regexkit.sourceforge.net/#RegexKitLite
rjstelling
+2  A: 

Have a try with CSRegex: http://www.cocoadev.com/index.pl?CSRegex

It is meant to be a quick and simple regex class that can be dropped into any Cocoa project with a minimum of fuss. It consists of only two files, and no external dependencies.

carlosb
+8  A: 
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '.*@.*\..*'"];
BOOL result = [predicate evaluateWithObject:@"[email protected]"];

According to the Predicate guide:

Matches
The left hand expression equals the right hand expression using a regex-style comparison according to ICU v3 (for more details see the ICU User Guide for Regular Expressions).

There's even an example written by Apple that can be found in the guide.

Instead of SELF you could also use a key path. (And possibly some other literals too.)

Georg
A: 

Another option is the libregex wrapper in the Google Toolbox for Mac: http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMRegex.h

smorgan
+2  A: 

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])+)\\])";
rjstelling