Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex solution for Objective-C

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

like image 962
Richard Stelling Avatar asked May 10 '09 13:05

Richard Stelling


1 Answers

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.)

like image 69
Georg Schölly Avatar answered Nov 05 '22 05:11

Georg Schölly