Regular expressions in an Objective-C Cocoa application

10 Solutions Collect From Internet About “Regular expressions in an Objective-C Cocoa application”

I noticed that as of iOS 4.0 Apple provides a NSRegularExpression class. Additionally, as of 10.7, the class is available under OS X.

  1. Yes, there’s no regex support in Cocoa. If you’re only interested in boolean matching, you can use NSPredicate which supports ICU regex syntax. But usually you’re interested in the position of the match or position of subexpressions, and you cannot get it with NSPredicate.
  2. As mentioned you can use regex POSIX functions. But they are considered slow, and the regex syntax is limited compared to other solutions (ICU/pcre).
  3. There are many OSS libraries, CocoaDev has an extensive list.
  4. RegExKitLite for example doesn’t requires any libraries, just add the .m and .h to your project.

    (My complaint against RegExKitLite is that it extends NSString via category, but it can be considered as a feature too. Also it uses the nonpublic ICU libraries shipped with the OS, which isn’t recommended by Apple.)

RegexKit is the best I’ve found yet. Very Cocoa:y. I’m using the “Lite” version in several of our iPhone apps:

sourceforge

lingonikorg

You can use the POSIX Regular Expressions library (Yay for a POSIX compliant OS). Try

man 3 regex

The cheap and dirty hack solution that I use to solve REGEX and JSON parsing issues is to create a UIWebView object and inject Javascript function(s) to do the parsing. The javascript function then returns a string of the value (or list of values) I care about. In fact, you can store a small library set of functions customized for particular tasks and then just call them as needed.

I don’t know if it this technique scales to huge volumes of repeated parsing requests, but for quick transactional stuff it gets the job done without depending on any extra external resources or code you might not understand.

I like the AGRegex framework which uses PCRE, handy if you are used to the PCRE syntax. The best version of this framework is the one in the Colloquy IRC client as it has been upgraded to use PCRE 6.7:

http://colloquy.info/project/browser/trunk/Frameworks/AGRegex

It’s very lightweight, much more so than RegExKit (although not as capable of course).

NSRegularExpression is available since Mac OS X v10.7 and IOS 4.0.

During my search on this topic I came across CocoaOniguruma which uses Oniguruma, the Regular Expression engine behind Ruby1.9 and PHP5. It seems a bit newer compared to the existing OregKit (in Japanese). Not sure how these stack up against other bindings.

Googling alittle, found this library:
RegexOnNSString

Open source library, containing functions like:

-(NSString *) stringByReplacingRegexPattern:(NSString *)regex withString:(NSString *) replacement caseInsensitive:(BOOL)ignoreCase

and using NSRegularExpression class. Quite easy to use and no need to worry about anything.

Please, note that NSRegularExpression is available since Mac OS X v10.7 and IOS 4.0, as Datasmid mentioned.

I make it easy. I add a new C++ file to my Objective C project, rename it as .mm, and then create a standard C++ class inside. Then, I make a static class method in the “public:” section for a C++ function that takes an NSString and returns an NSString (or NSArray, if that’s what you want). I then convert NSString to C++ std::string like so:

// If anyone knows a more efficient way, let me know in the comments.
// The "if" condition below is because ObjC crashes if converting to
// std::string if the string is nil or empty.
// assume #include <string>
std::string s = "";
if (([sInput != nil]) && (!([sInput isEqualTo:@""]))) {
  std::string sTemp([sInput UTF8String]);
  s = sTemp;
}

From there, I can use regex_replace like so:

// assume #include <regex>
std::string sResult = std::regex_replace(sSource,sRegExp,sReplaceWith);

Then, I can convert that std::string back into an NSString with:

NSString *sResponse2 = @(sResult.c_str());

If you’re only using this C++ just for this function, then you may find it suitable to call this file extra.mm (class name Extra) and put this static class method in, and then add other static class methods when the situation arrives where it just makes sense to do it in C++ because it’s less hassle in some cases. (There are cases where ObjC does something with less lines of code, and some cases where C++ does it with less lines of code.)

P.S. Still yet another way with this is to use a .mm file but make an Objective C wrapper around the use of std::string and std::regex_replace() (or regex_match()).