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: Difference between revisions - iPhone Development Wiki

CPRegularExpression: Difference between revisions

From iPhone Development Wiki
(Created page with '{{occlass|library=AppSupport.framework}} CPRegularExpression is a wrapper around the POSIX [http://www.opengroup.org/onlinepubs/007908799/xsh/regex.h.html regex.h] library. …')
 
m (Reordering)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{occlass|library=AppSupport.framework}}
[[CPRegularExpression]] is a wrapper around the POSIX [http://www.opengroup.org/onlinepubs/007908799/xsh/regex.h.html regex.h] library.


[[CPRegularExpression]] is a wrapper around the POSIX [http://www.opengroup.org/onlinepubs/007908799/xsh/regex.h.html regex.h] library. Example use:
== Example code ==


<source lang="objc">
<source lang="objc">
NSString* test = [NSString stringWithContentsOfFile:@"TestFile.txt"];
NSString* test = [NSString stringWithContentsOfFile:@"TestFile.txt"];
NSRange curRange = NSMakeRange(0, [test length]);
 
CPRegularExpression* re = [CPRegularExpression regularExpressionWithString:@"([-_[:alpha:]]+)=['\"]?([^'\">[:blank:]]*)['\"]?"];
CPRegularExpression* re = [CPRegularExpression regularExpressionWithString:@"([-_[:alpha:]]+)=['\"]?([^'\">[:blank:]]*)['\"]?"];
NSRange curRange = NSMakeRange(0, [test length]);
NSUInteger subexprCount = [re numberOfSubexpressions];
NSUInteger subexprCount = [re numberOfSubexpressions];
NSRange subexprs[subexprCount];
NSRange subexprs[subexprCount];
while (1) {
while (1) {
NSRange newRange = [re matchedRangeForString:test range:curRange subexpressionRanges:subexprs count:subexprCount];
NSRange newRange = [re matchedRangeForString:test range:curRange subexpressionRanges:subexprs count:subexprCount];
if (newRange.location == NSNotFound)
if (newRange.location == NSNotFound) {
break;
break;
else {
} else {
NSLog(@"%@ -> %@", [test substringWithRange:subexprs[0]], [test substringWithRange:subexprs[1]]);
NSLog(@"%@ -> %@", [test substringWithRange:subexprs[0]], [test substringWithRange:subexprs[1]]);
Line 24: Line 24:
</source>
</source>


== Header ==
== External links ==
* http://github.com/kennytm/iphone-private-frameworks/blob/master/AppSupport/CPRegularExpression.h
 
{{IPFHeader|AppSupport}}

Latest revision as of 18:36, 26 October 2014

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