Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

NSSTring_RegEx: Add support for regular expressions to NSString

This is an extension found via Cocoadev, created by John R Chang. Thanks!
  • Loading branch information...
commit e79c11b9a1fc682ba694de878133768b9e149f2f 1 parent aa43e18
Pieter de Bie authored June 11, 2009
6  GitX.xcodeproj/project.pbxproj
@@ -59,6 +59,7 @@
59 59
 		F53FF2050E7ABB5300389171 /* PBGitRevSpecifier.m in Sources */ = {isa = PBXBuildFile; fileRef = F53FF2040E7ABB5300389171 /* PBGitRevSpecifier.m */; };
60 60
 		F56174570E058893001DCD79 /* PBGitTree.m in Sources */ = {isa = PBXBuildFile; fileRef = F56174560E058893001DCD79 /* PBGitTree.m */; };
61 61
 		F56244090E9684B0002B6C44 /* PBUnsortableTableHeader.m in Sources */ = {isa = PBXBuildFile; fileRef = F56244080E9684B0002B6C44 /* PBUnsortableTableHeader.m */; };
  62
+		F562C8870FE1766C000EC528 /* NSString_RegEx.m in Sources */ = {isa = PBXBuildFile; fileRef = F562C8860FE1766C000EC528 /* NSString_RegEx.m */; };
62 63
 		F56524BB0E02D22D00F03B52 /* NSFileHandleExt.m in Sources */ = {isa = PBXBuildFile; fileRef = F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */; };
63 64
 		F56524F00E02D45200F03B52 /* PBGitCommit.m in Sources */ = {isa = PBXBuildFile; fileRef = F56524EF0E02D45200F03B52 /* PBGitCommit.m */; };
64 65
 		F56526240E03D85900F03B52 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F56526230E03D85900F03B52 /* WebKit.framework */; };
@@ -213,6 +214,8 @@
213 214
 		F56174560E058893001DCD79 /* PBGitTree.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitTree.m; sourceTree = "<group>"; };
214 215
 		F56244070E9684B0002B6C44 /* PBUnsortableTableHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBUnsortableTableHeader.h; sourceTree = "<group>"; };
215 216
 		F56244080E9684B0002B6C44 /* PBUnsortableTableHeader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBUnsortableTableHeader.m; sourceTree = "<group>"; };
  217
+		F562C8850FE1766C000EC528 /* NSString_RegEx.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSString_RegEx.h; sourceTree = "<group>"; };
  218
+		F562C8860FE1766C000EC528 /* NSString_RegEx.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSString_RegEx.m; sourceTree = "<group>"; };
216 219
 		F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSFileHandleExt.m; sourceTree = "<group>"; };
217 220
 		F56524BA0E02D22D00F03B52 /* NSFileHandleExt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NSFileHandleExt.h; sourceTree = "<group>"; };
218 221
 		F56524EE0E02D45200F03B52 /* PBGitCommit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitCommit.h; sourceTree = "<group>"; };
@@ -510,6 +513,8 @@
510 513
 				F5AD56780E79B78100EDAAFE /* PBCommitList.m */,
511 514
 				F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */,
512 515
 				F56524BA0E02D22D00F03B52 /* NSFileHandleExt.h */,
  516
+				F562C8850FE1766C000EC528 /* NSString_RegEx.h */,
  517
+				F562C8860FE1766C000EC528 /* NSString_RegEx.m */,
513 518
 				F57CC38F0E05DDF2000472E2 /* PBEasyPipe.h */,
514 519
 				F57CC3900E05DDF2000472E2 /* PBEasyPipe.m */,
515 520
 				F5DFFA6A0E075D8800617813 /* PBEasyFS.h */,
@@ -842,6 +847,7 @@
842 847
 				47DBDB580E94EDE700671A1E /* DBPrefsWindowController.m in Sources */,
843 848
 				47DBDB670E94EE8B00671A1E /* PBPrefsWindowController.m in Sources */,
844 849
 				47DBDBCA0E95016F00671A1E /* PBNSURLPathUserDefaultsTransfomer.m in Sources */,
  850
+				F562C8870FE1766C000EC528 /* NSString_RegEx.m in Sources */,
845 851
 			);
846 852
 			runOnlyForDeploymentPostprocessing = 0;
847 853
 		};
34  NSString_RegEx.h
... ...
@@ -0,0 +1,34 @@
  1
+//
  2
+//  NSString_RegEx.h
  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 <Foundation/Foundation.h>
  10
+
  11
+/*
  12
+	For regular expression help, see re_format(7) man page.
  13
+*/
  14
+
  15
+@interface NSString (RegEx)
  16
+
  17
+/*
  18
+	Common <options> are REG_ICASE and REG_NEWLINE.  For other possible option flags,
  19
+	see regex(3) man page.  You don't need to specify REG_EXTENDED.
  20
+	
  21
+	<nmatch> is the number of subexpressions to match.
  22
+	Returns an array of strings.  The first string is the matching substring,
  23
+		the remaining are the matching subexpressions, up to nmatch+1 number.
  24
+
  25
+	If nmatch is -1, works like grep.  Returns an array containing self if matching.
  26
+		
  27
+	Returns nil if regular expression does not match or if an error has occurred.
  28
+*/
  29
+- (NSArray *) substringsMatchingRegularExpression:(NSString *)pattern count:(int)nmatch
  30
+	options:(int)options ranges:(NSArray **)ranges error:(NSError **)error;
  31
+
  32
+- (BOOL) grep:(NSString *)pattern options:(int)options;
  33
+
  34
+@end
95  NSString_RegEx.m
... ...
@@ -0,0 +1,95 @@
  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 notes on commit e79c11b

Please sign in to comment.
Something went wrong with that request. Please try again.