Basically you have two options
Regular expressions
NSString *string = @"<aNodee thsisjijdsnjdnjsd>";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<(\\S+)( .*)>" options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
[matches enumerateObjectsUsingBlock:^(NSTextCheckingResult *result, NSUInteger idx, BOOL *stop) {
NSString* nodeName = [string substringWithRange:[result rangeAtIndex:1]];
NSString* value = [string substringWithRange:[result rangeAtIndex:2]];
NSLog(@"%@ %@",nodeName, value);
}];
Note, that you shouldn't parse complex html with Regular Expressions.
NSScanner
NSScanner *scanner = [NSScanner scannerWithString:string];
BOOL recordingValue = NO;
NSMutableString *valueString = [@"" mutableCopy];
[scanner setScanLocation:0];
while (![scanner isAtEnd]) {
NSString *charAtlocation = [string substringWithRange:NSMakeRange([scanner scanLocation], 1)];
if ([charAtlocation isEqualToString:@" "]){
recordingValue = YES;
[valueString appendString:@" "];
} else{
if ([charAtlocation isEqualToString:@">"]){
recordingValue = NO;
} else if (recordingValue) {
[valueString appendString:charAtlocation];
}
}
[scanner setScanLocation:[scanner scanLocation]+1];
} ;
NSLog(@"Scanner approach: %@", valueString);
NSLog(@"Scanner approach: %@", [string stringByReplacingOccurrencesOfString:valueString withString:@""]);
Complete command line based example
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *string = @"<aNodee thsisjijdsnjdnjsd> ";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<([a-zA-z]+)( .*)>" options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
[matches enumerateObjectsUsingBlock:^(NSTextCheckingResult *result, NSUInteger idx, BOOL *stop) {
NSString* nodeName = [string substringWithRange:[result rangeAtIndex:1]];
NSString* value = [string substringWithRange:[result rangeAtIndex:2]];
NSLog(@"Regex approach: %@ %@",nodeName, value);
NSLog(@"Regex approach: %@", [string stringByReplacingOccurrencesOfString:value withString:@""]);
}];
NSScanner *scanner = [NSScanner scannerWithString:string];
BOOL recordingValue = NO;
NSMutableString *valueString = [@"" mutableCopy];
[scanner setScanLocation:0];
while (![scanner isAtEnd]) {
NSString *charAtlocation = [string substringWithRange:NSMakeRange([scanner scanLocation], 1)];
if ([charAtlocation isEqualToString:@" "]){
recordingValue = YES;
[valueString appendString:@" "];
} else{
if ([charAtlocation isEqualToString:@">"]){
recordingValue = NO;
} else if (recordingValue) {
[valueString appendString:charAtlocation];
}
}
[scanner setScanLocation:[scanner scanLocation]+1];
} ;
NSLog(@"Scanner approach: %@", valueString);
NSLog(@"Scanner approach: %@", [string stringByReplacingOccurrencesOfString:valueString withString:@""]);
}
return 0;
}
Output:
Regex approach: aNodee thsisjijdsnjdnjsd
Regex approach: <aNodee>
Scanner approach: thsisjijdsnjdnjsd
Scanner approach: <aNodee>