|
| 1 | +// |
| 2 | +// NSString_RegEx.m |
| 3 | +// |
| 4 | +// Created by John R Chang on 2005-11-08. |
| 5 | +// This code is Creative Commons Public Domain. You may use it for any purpose whatsoever. |
| 6 | +// http://creativecommons.org/licenses/publicdomain/ |
| 7 | +// |
| 8 | + |
| 9 | +#import "NSString_RegEx.h" |
| 10 | +#include <regex.h> |
| 11 | + |
| 12 | + |
| 13 | +@implementation NSString (RegEx) |
| 14 | + |
| 15 | +- (NSArray *) substringsMatchingRegularExpression:(NSString *)pattern count:(int)nmatch options:(int)options ranges:(NSArray **)ranges error:(NSError **)error |
| 16 | +{ |
| 17 | + options |= REG_EXTENDED; |
| 18 | + if (error) |
| 19 | + *error = nil; |
| 20 | + |
| 21 | + int errcode = 0; |
| 22 | + regex_t preg; |
| 23 | + regmatch_t * pmatch = NULL; |
| 24 | + NSMutableArray * outMatches = nil; |
| 25 | + |
| 26 | + // Compile the regular expression |
| 27 | + errcode = regcomp(&preg, [pattern UTF8String], options); |
| 28 | + if (errcode != 0) |
| 29 | + goto catch_error; // regcomp error |
| 30 | + |
| 31 | + // Match the regular expression against substring self |
| 32 | + pmatch = calloc(sizeof(regmatch_t), nmatch+1); |
| 33 | + errcode = regexec(&preg, [self UTF8String], (nmatch<0 ? 0 : nmatch+1), pmatch, 0); |
| 34 | + |
| 35 | + /*if (errcode == REG_NOMATCH) |
| 36 | + { |
| 37 | + outMatches = [NSMutableArray array]; |
| 38 | + goto catch_exit; // no match |
| 39 | + }*/ |
| 40 | + if (errcode != 0) |
| 41 | + goto catch_error; // regexec error |
| 42 | + |
| 43 | + if (nmatch == -1) |
| 44 | + { |
| 45 | + outMatches = [NSArray arrayWithObject:self]; |
| 46 | + goto catch_exit; // simple match |
| 47 | + } |
| 48 | + |
| 49 | + // Iterate through pmatch |
| 50 | + outMatches = [NSMutableArray array]; |
| 51 | + if (ranges) |
| 52 | + *ranges = [NSMutableArray array]; |
| 53 | + int i; |
| 54 | + for (i=0; i<nmatch+1; i++) |
| 55 | + { |
| 56 | + if (pmatch[i].rm_so == -1 || pmatch[i].rm_eo == -1) |
| 57 | + break; |
| 58 | + |
| 59 | + NSRange range = NSMakeRange(pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so); |
| 60 | + NSString * substring = [self substringWithRange:range]; |
| 61 | + [outMatches addObject:substring]; |
| 62 | + |
| 63 | + if (ranges) |
| 64 | + { |
| 65 | + NSValue * value = [NSValue valueWithRange:range]; |
| 66 | + [(NSMutableArray *)*ranges addObject:value]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +catch_error: |
| 71 | + if (errcode != 0 && error) |
| 72 | + { |
| 73 | + // Construct error object |
| 74 | + NSMutableDictionary * userInfo = [NSMutableDictionary dictionary]; |
| 75 | + char errbuf[256]; |
| 76 | + int len = regerror(errcode, &preg, errbuf, sizeof(errbuf)); |
| 77 | + if (len > 0) |
| 78 | + [userInfo setObject:[NSString stringWithUTF8String:errbuf] forKey:NSLocalizedDescriptionKey]; |
| 79 | + *error = [NSError errorWithDomain:@"regerror" code:errcode userInfo:userInfo]; |
| 80 | + } |
| 81 | + |
| 82 | +catch_exit: |
| 83 | + if (pmatch) |
| 84 | + free(pmatch); |
| 85 | + regfree(&preg); |
| 86 | + return outMatches; |
| 87 | +} |
| 88 | + |
| 89 | +- (BOOL) grep:(NSString *)pattern options:(int)options |
| 90 | +{ |
| 91 | + NSArray * substrings = [self substringsMatchingRegularExpression:pattern count:-1 options:options ranges:NULL error:NULL]; |
| 92 | + return (substrings && [substrings count] > 0); |
| 93 | +} |
| 94 | + |
| 95 | +@end |
0 commit comments