What is Regex ?
Regex stands for Regular Expressions. It consists of constants and operators that denote sets of strings and operations over these sets, respectively. The definition is as follows :

Given a finite alphabet S, the following constants are defined:

Notation in Regular ExpressionsMeaning of the Notation
ØEmpty Set
eEmpty String
a in SCharacter a in set S

A Regular Expression also supports operations like:

OperationsMeaning of the Operation
RS denoting the set { aß | a in R and ß in S }
e.g. R = {“a” , “b”} and S = {“c”,”d”} then RS = {“ac”,”ad”,”bc”,bd”}
Concatenation(can say, Cartesian Product )
R|S denoting the set R union S
e.g.R = {“a”,”b”} and s = {“a”,”c”} then R|S = {“a”,”b”,”c”}
R* denoting the set as follows:
e.g. R = {“abc”, “d”} then R* = {e,”abc”,”d”,”abcd”,”dabc”,……
Kleene Star

Here are some examples:

1.a | bc represents set of strings containing either a or bc in a string
2.(a | b)* represents set { e ,a ,b , aa , bb , aaa , bbb ,….}

We can use these Regular Expressions in our iphone Program by using the RegexKitLite Class. So, lets get started with the RegexKitLite.

In this Blog, we are going to see :
1.What is RegexKitLite ?
2.Adding RegexKitLite to your Project.
3.Use of RegexKitLite Framework(Example code).

What is RegexKitLite Framework?
This Framework, basically provides a small set of extra methods for NSString class. These methods are to be used when regular expressions are to be used. We can use these methods in case of matching with range of strings with respect to the Regular Expression. In such a scenario, searching using substring can be extremely efficient.

Adding RegexKitLite to your Project:
1.Download the source code of RegexKitLite from here.
2.Browse these new files named “RegexKitLite.h” and “RegexKitLite.m” which were downloaded and add the same to your Project. This can be shown as below:

Add -> Existing Files….

Note : Make Sure that “Copy items into Destination’s Group Folder(if needed)” option is checkmarked

iPhone Regular Expression Tutorial: RegexKitLite Framework

3.RegexKitLite uses the regular expression provided by the ICU library that ships with Mac OS X and since RegexKitLite uses the linker /usr/lib/libicucore.dylib ICU shared Library,we might get errors in our program within core Library files. To avoid this,please follow the steps below:

1.Go to Project -> Edit Project Settings

iPhone Regular Expression Tutorial: RegexKitLite Framework

2.Type in linker in the search text and select the “Other Linker Flags” option.

iPhone Regular Expression Tutorial: RegexKitLite Framework

3.Add a new linker flag named -licucore

iPhone Regular Expression Tutorial: RegexKitLite Framework

iPhone Regular Expression Tutorial: RegexKitLite Framework

Finally, we are done adding RegexKitLite to our Project.
Use of RegexKitLite Framework(Example code) :
Since Regex adds methods to existing methods of NSString class, we are going to see some examples that can be executed using Regex. Now,Since the project has been setup in your application(WindowBasedApplication), just import the ”RegexKitLite.h” and add this code to your applicationDidFinishLaunching method.

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
//Extraction of valid mail ids
NSString * searchString = @" para.g@gmail.com sourabh_84368@gmail.com abc@def.in abcd@yahoomail.com mobisoft@mail.in andy@rediffmail.com";
NSString *regexString = @"[a-z0-9_.%]+@[a-z0-9_.%]+\\.[a-z][a-z][a-z]";
NSArray  *matchArray   = nil;
matchArray = [searchString componentsMatchedByRegex:regexString];
NSLog(@"matchArray: %@", matchArray);
// Override point for customization after application launch
[window makeKeyAndVisible];

Just run your application and check your console the output will be:

iPhone Regular Expression Tutorial: RegexKitLite Framework

Thus,we created an array which shown extracting strings which end with the character “g” from a main string. Similarly we can create many Regular Expressions use to extract the data that is required. For more information click here.

You can download the source code here.