简体   繁体   English

从图形API URL获取Facebook用户ID的正则表达式?

[英]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? 什么是将用户ID从以下格式的URL中提取出来的正则表达式? Eg in the example below, I am looking to get "1227627" as a match after applying the regex. 例如,在下面的示例中,我希望在应用正则表达式后获得“ 1227627”作为匹配项。

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. 注意:我将在iOS /目标c中使用它。

For some background-- I'm using the graphi API batch request. 对于某些背景-我正在使用graphi API批处理请求。 A batch request can contain up to 20 individual requests, and unfortunately the individual responses do not return the request URL. 一个批处理请求最多可以包含20个单独的请求,但是不幸的是,单个响应不返回请求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. 我可以跟踪所有单独的请求,但是解析出该请求所对应的用户ID会更简单。

You could use 'https?://graph.facebook.com/([0-9]+)/' and the number would be the first capturing group. 您可以使用“ https?://graph.facebook.com/([0-9] +)/”,该数字将是第一个捕获组。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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