views:

11932

answers:

6

Initial Googling indicates that there's no built-in way to do regular expressions in an Objective-C Cocoa application.

So four questions:

  1. Is that really true?

  2. Are you kidding me?

  3. Ok, then is there a nice open-source library you recommend?

  4. What are ways to get close enough without importing a library, perhaps with the NSScanner class?

+8  A: 

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

man 3 regex
Adam Wright
oh, I see. this is a way to do it in straight C, which should presumably work in any objective-C app. cool, thanks! is that basically the accepted way to do this?
dreeves
It's a way, that doesn't require any additional dependencies. There are other choices, in terms of open source libraries that you could import (PCRE, for Perl regexes, The Boost RegEx library if you're using Obj-C++, or others listed in other answers).
Adam Wright
Any disadvantages to mixing straight C with Objective-C? Could you maybe include a code snippet for converting to and from NSString? thanks again!
dreeves
Objective-C is built on top of C, so you're not really mixing anything. Most people use a library though, since it offers an API that's easier to use.
Marc Charbonneau
And because POSIX regex functions only work with ASCII strings.
Tom Dalling
+13  A: 

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

http://regexkit.sourceforge.net/ http://lingonikorg.com

avocade
I second RegexKit Lite. Very nice!
Dave Dribin
Cool, the more people that use it, the better it'll probably become!
avocade
+4  A: 

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

Rob Keniger
Why less capable if it has full perl-compatible regex?
dreeves
The associated Objective-C helper methods are not nearly as extensive as those in RegExKit, however they are fine for most purposes.
Rob Keniger
+25  A: 
  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.)

mfazekas
Keep in mind that POSIX regex functions don't work with unicode (ASCII only).
Tom Dalling
`NSPredicate` has a couple of caveats when it comes to using the `MATCHES` operator, such as it has to match the entire string (and can't match against only a substring). I heartily recommend RegexKitLite.
Dave DeLong
@mfazekas FYI that NSPredicate url is a broken link
taber
+2  A: 

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.

newtonapple
+8  A: 

I noticed that as of iOS 4.0 Apple provides a NSRegularExpression class:

http://developer.apple.com/iphone/library/documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html

NesReqSej
Should this be the new accepted answer at some point?
dreeves