简体   繁体   中英

Regular expression to get a Facebook user ID from a graph API URL?

What is a regular expression that will pull the user ID out of a URL of the following format? Eg in the example below, I am looking to get "1227627" as a match after applying the regex.

https://graph.facebook.com/1227627/movies?access_token=[access_token]&limit=5000&offset=5000&__after_id=103108306395658

Note: I will be using this in iOS/objective c.

For some background-- I'm using the graphi API batch request. A batch request can contain up to 20 individual requests, and unfortunately the individual responses do not return the request URL. I could keep track of all individual requests but it would be simpler to parse out the user ID the request corresponded to this way.

You could use 'https?://graph.facebook.com/([0-9]+)/' and the number would be the first capturing group.

Something along the lines of:

// input URL
NSString *urlString = @"https://graph.facebook.com/1227627/movies?access_token=access_token]&limit=5000&offset=5000&__after_id=103108306395658";

// construct the regex
NSRegularExpression *regex = [NSRegularExpression 
      regularExpressionWithPattern:@"https?://graph\\.facebook\\.com/([0-9]+)/"  
                           options:NSRegularExpressionSearch 
                             error:nil];
// match against url
NSArray *matches = [regex matchesInString:urlString
                                  options:0
                                    range:NSMakeRange(0, [urlString length])];
// extract capturing group 1
for (NSTextCheckingResult *match in matches) {
    NSRange matchRange = [match rangeAtIndex:1];
    NSString *matchString = [urlString substringWithRange:matchRange];
    NSLog(@"%@", matchString);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM