Simple Example-Tutorial: REGEX (Regular Expressions) in iPhone app to find and replace a value in NSString
Since there is no native ability in Objective C in the Cocoa Framework to use REGEX (Regular Expressions), you need to use a framework. I found that RegexKitLite was the perfect solution for my app. You can use it in 3 simple steps:
1. Add the RegexKit Framework to your app’s project in XCode
You can download the source at http://regexkit.sourceforge.net/#Downloads. Then just add the RegexKitLite.h and RegexKitLite.m files to your project. Also, you’ll need to add the libicucore.dylib Framework to your project. This can be done by right clicking on the Frameworks folder in xCode, selecting Add > Add Existing Framework and then selecting to add libicucore.dylib. Be sure to import the RegexKitLite.h file wherever you plan to use it: #import “RegexKitLite.h”.
2. Create an NSString with the Regular Expression
For example I needed a regular expression that would find all the HTML tags in an NSString and get rid of them by replacing them with an empty string “”. It looks like this:
NSString *myregex = @”<[^>]*>”; //regex to remove any html tag
3. Use the StringByReplacingOccurencesOfRegex method to replace your NSString
I think the best way to explain this is to just show it:
NSString *description = @”<html><body><p>This is the description! </p></body></html>”;
description = [description stringByReplacingOccurrencesOfRegex:myregex withString:@""];
The final description variable will just say “This is the description! ” as the tags are stripped out. All you have to do is plug in your regex NSString variable from step 2 where I have “myregex” and plug in whatever string you want to replace it with where I have @”" (an empty string to delete the occurrence rather than replace it).
Hope this is helpful- good luck!


Wow !!!! It’s great. Work properly. Thanks ;;))