[英]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.