Regular Expressions in Objective-C

This class allows you to use very powerfull regular expressions (because it’s based on regexec from C) in an object oriented way.
Thanks to the post of David Teare, I finally find a very easy and powerfull solution for using regular expressions in Objective-C. David’s code only returns a boolean to indicate if the NSString was matching the given regular expression. I improve its method by returning a NSMutableArray with the different sub-expressions that lead to a match. Unfortunately I wasn’t able to return nil when the sub-expression doesn’t produce a match, instead I return the empty NSString @””.

Download Regular Expression Source Code

[Updated the 16.05.2007] Thanks to everybody for you remarks, I updated the source file accordingly. I also add a example script to demonstrate the use of the method.

Download Regular Expression Source Code version 1.1

[Updated the 01.01.2008] After receiving a reader’s mail about the code’s licencing, I clarify the situation and add the GPL licence in the header as well as the full licence in the package.

Download  Regular Expression Source Code version 1.1

[Update 22.02.2009] Hey I just discovered that TwitterFon, YMail and swisssms are using this class. Cool isn’t it.

[Updated the 08.04.2009] Bugfix the behavior when working on NSString with UTF8 encoded chars. Thanks to Ian Atha & soniccat for the correction

Download Regular Expression Source Code version 1.2

  • soniccat
    Excellent library, but when i use multi-byte character ( russian in my example ) function crash. That happen because pmatch[i].rm_eo store a number of byte in string. In my example russian character have 2 byte and in
    self substringToIndex:pmatch[i].rm_eo

    i have out of range exception because pmatch[i].rm_eo > [self length]
    I write that for resolved that problem:

    size_t buffSize = pmatch[i].rm_eo - pmatch[i].rm_so + 1;
    char* buff = (char*)malloc( buffSize );
    memset(buff,0,sizeof(char)*buffSize );
    memcpy(buff,[self UTF8String]+pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so );

    [substring addObject: [NSString stringWithUTF8String:buff] ];
    free( buff );

  • BTW, you can add NSNull for empty substrings, but I agree that @"" is better.
  • Thanks!

    There's a bug:

    if (0 == regcomp(&re, regex, REG_EXTENDED)) {

    should be

    if (0 == regcomp(&re, [regex UTF8String], REG_EXTENDED)) {

    (You definitely need to turn on "Stop on all warnings" compile option ;)

    Also, you have "false" instead of "NO" in the beginning.

    Anyway, thanks for the code!
  • Gavin
    Hey Well done, however I cannot work out how to get the results, do you have an example on how to get the returned array ?
  • Hey, cool! I'm glad my code sample helped you out.

    I was amazed that Cocoa/objective C didn't have an easier way to do regex; I guess I have been spoiled by Ruby and its =~ operator :)
blog comments powered by Disqus