[英]Regular expression to get a Facebook user ID from a graph API URL?
什么是将用户ID从以下格式的URL中提取出来的正则表达式? 例如,在下面的示例中,我希望在应用正则表达式后获得“ 1227627”作为匹配项。
https://graph.facebook.com/1227627/movies?access_token=[access_token]&limit=5000&offset=5000&__after_id=103108306395658
注意:我将在iOS /目标c中使用它。
对于某些背景-我正在使用graphi API批处理请求。 一个批处理请求最多可以包含20个单独的请求,但是不幸的是,单个响应不返回请求URL。 我可以跟踪所有单独的请求,但是解析出该请求所对应的用户ID会更简单。
您可以使用“ https?://graph.facebook.com/([0-9] +)/”,该数字将是第一个捕获组。
类似于以下内容:
// 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.