Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Log in or create an account to edit The Apple Wiki.

Dev:CPRegularExpression

From The Apple Wiki

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

As of iOS 4.0, NSRegularExpression exists as public API, making use of the more complete ICU4C regex engine. Use this instead of the private CPRegularExpression.

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