使用下一个方法(注意:使用 ARC):
-(NSMutableArray*)scanInString:(NSString*)string beforeValue:(NSString*)beforeValue afterValue:(NSString*)afterValue{
NSMutableArray *foundedResults = [NSMutableArray new];
NSString *allString = [[NSString alloc] initWithString:string];
BOOL isHavingValue = NO;
do{
NSRange beforeRange = [allString rangeOfString:beforeValue];
if(beforeRange.location != NSNotFound){
int beforePos = beforeRange.location + beforeRange.length;
allString = [allString substringFromIndex:beforePos];
NSRange afterRange = [allString rangeOfString:afterValue];
if(afterRange.location != NSNotFound){
isHavingValue = YES;
int afterPos = afterRange.location;
NSRange valueRange = NSMakeRange(0, afterPos);
NSString *value = [allString substringWithRange:valueRange];
if(value){
[foundedResults addObject:value];
}
}
else{
isHavingValue = NO;
}
}
else{
isHavingValue = NO;
}
}while (isHavingValue);
return foundedResults;
}
来电:
NSString *string = @"<INPUT TYPE=\"HIDDEN\" NAME=\"one\" VALUE=\"getthisvalue\"><INPUT TYPE=\"HIDDEN\" NAME=\"two\" VALUE=\"ILYA2606\"><INPUT TYPE=\"HIDDEN\" NAME=\"three\" VALUE=\"XXX\"><INPUT TYPE=\"HIDDEN\" NAME=\"four\" VALUE=\"getthisvalue\">";
NSLog(@"results = %@", [self scanInString:string beforeValue:@"VALUE=\"" afterValue:@"\""]);