i try to extract simple string from string with out success it looks simple but the result alwways as
the string
here is what i have :
-(NSString*) ExtractArtistNameFromString:(NSString*) line
{
NSString* substringForMatch = @""
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\d+\\s+(.*)"
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error) {
NSLog(@"Error:%@", error);
}
NSArray *matches = [regex matchesInString:line
options:0
range:NSMakeRange(0, [line length])];
NSUInteger elements = [matches count];
NSLog(@"numberOfMatches:%u",elements);
if(elements > 0)
{
for(NSTextCheckingResult *match in matches)
{
NSString* substringForMatch = [line substringWithRange:match.range];
NSUInteger slen = [substringForMatch length];
NSLog(@"Extracted : %@",substringForMatch);
NSLog(@"%u",slen);
NSLog(@"%@",substringForMatch);
}
}
return substringForMatch;
}
input string is :
1 Barsotti, Marcel
what im trying to extract is the name that is : Barsotti, Marcel
and when i run the app i get this result :
2013-01-29 11:31:24.056 file_parser[2496] numberOfMatches:1
2013-01-29 11:31:24.062 file_parser[2496] Extracted : 1 Barsotti, Marcel
2013-01-29 11:31:24.064 file_parser[2496] 18
2013-01-29 11:31:24.066 file_parser[2496] 1 Barsotti, Marcel
im using GNUsetup in windows , the make command is:
make CC=clang
I see what you’re trying to do. The documentation for NSTextCheckingResult shows that a result can have multiple ranges within it, yet you’re only referencing the first range.
For example, your RegEx and your string will have 1 match, with 1 capture group within it. These are:
Whilst you only have one
NSTextCheckingResult, there are multiple ranges within it. You were usingThis will return the first result listed above. What you want is the second. In order to get this, you need to use
To make sure you don’t exceed the
rangeAtIndex:, the propertynumberOfRangesexists to allow you to keep within bounds. Remember,[match rangeAtIndex:0]is the same atmatch.range.