Search code examples
iphoneregexkitlite

tooltipHtml - extract from google maps call


" (2.1\x26#160;mi / 7 mins)"

I have the above string and I need to get to the distance and time only. How would I go about doing this using the RegexKitLite script? Ideally, the below is what I'm looking for:

time = 7 mins distance = 2.1 miles

Thanks


Solution

  • Not with RegexKitLite script but I've done it this way:

    NSString* tooltipHtml = [apiResponse stringByMatching:@"tooltipHtml:\\"([^\\"]*)\\"" capture:1L];

    NSLog(@"tooltip Html: %@", tooltipHtml);
    
    
    int leftParanthesis = [tooltipHtml rangeOfString:@"("].location;
    int inverseSlash = [tooltipHtml rangeOfString:@"\\"].location;
    int slash = [tooltipHtml rangeOfString:@"/"].location;
    int semiColumn = [tooltipHtml rangeOfString:@";"].location;
    int rightParanthesis = [tooltipHtml rangeOfString:@")"].location;
    
    
    NSRange distanceRange = NSMakeRange(leftParanthesis+1, inverseSlash-leftParanthesis-1);
    NSString *distance = [tooltipHtml substringWithRange:distanceRange];
    
    NSRange distanceTypeRange = NSMakeRange(semiColumn+1, slash-semiColumn-1);
    NSString *distanceType = [tooltipHtml substringWithRange:distanceTypeRange];
    
    NSRange timeRange = NSMakeRange(slash+1, rightParanthesis-slash-1);
    NSString *time = [tooltipHtml substringWithRange:timeRange];
    NSString *distanceCompact = [distance stringByAppendingString:distanceType];
    
    NSLog(@"Distance %@:", distance);
    NSLog(@"Distance type %@:", distanceType);
    NSLog(@"Time %@:", time);
    NSLog(@"distance Compact %@:", distanceCompact);