Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/extensions/Variables/includes/ExtVariables.php on line 198
CPRegularExpression - iPhone Development Wiki

CPRegularExpression

From iPhone Development Wiki
Revision as of 18:36, 26 October 2014 by Uroboro (talk | contribs) (Reordering)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

CPRegularExpression is a wrapper around the POSIX regex.h library.

Example code

NSString* test = [NSString stringWithContentsOfFile:@"TestFile.txt"];
NSRange curRange = NSMakeRange(0, [test length]);

CPRegularExpression* re = [CPRegularExpression regularExpressionWithString:@"([-_[:alpha:]]+)=['\"]?([^'\">[:blank:]]*)['\"]?"];
NSUInteger subexprCount = [re numberOfSubexpressions];
NSRange subexprs[subexprCount];
while (1) {
	NSRange newRange = [re matchedRangeForString:test range:curRange subexpressionRanges:subexprs count:subexprCount];
	if (newRange.location == NSNotFound) {
		break;
	} else {
		NSLog(@"%@ -> %@", [test substringWithRange:subexprs[0]], [test substringWithRange:subexprs[1]]);
		
		// Changing the range alone is buggy.
		test = [test substringFromIndex:newRange.location + newRange.length];
		curRange.length = [test length];
	}
}

External links