尝试将 Ruby 代码移植到 ObjC 代码时遇到问题
红宝石:
clean_url = original_url.gsub(/\\u0026[^&]*/, "")
执行:
original_url = http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0&ms=au&expire=1368735912&id=e934f5f5c0743533&fexp=919374,909926,916713,916611,901474,924605,901208,929123,929915,929906,925714,929119,931202,928017,912518,911416,906906,904476,930807,919373,906836,933701,900345,926403,912711,929606,910075&sparams=cp,id,ip,ipbits,itag,ratebypass,source,upn,expire&sver=3&cp=U0hVTVdOU19GTENONV9PSFdKOnZFc0Uyc21YTVQw&ratebypass=yes&mv=m&source=youtube&itag=43&newshard=yes&mt=1368711866&ipbits=8&ip=92.114.198.83&key=yt1\u0026quality=medium\u0026type=video/webm&signature=AB8A6D618BDC38AF9D2E81916B863B724D2F12B6.8876CF4E106820B6443B4B06055BF90FD74B5794\u0026fallback_host=tc.v19.cache7.c.youtube.com,url=http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0
clean_url = http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0&ms=au&expire=1368735912&id=e934f5f5c0743533&fexp=919374,909926,916713,916611,901474,924605,901208,929123,929915,929906,925714,929119,931202,928017,912518,911416,906906,904476,930807,919373,906836,933701,900345,926403,912711,929606,910075&sparams=cp,id,ip,ipbits,itag,ratebypass,source,upn,expire&sver=3&cp=U0hVTVdOU19GTENONV9PSFdKOnZFc0Uyc21YTVQw&ratebypass=yes&mv=m&source=youtube&itag=43&newshard=yes&mt=1368711866&ipbits=8&ip=92.114.198.83&key=yt1&signature=AB8A6D618BDC38AF9D2E81916B863B724D2F12B6.8876CF4E106820B6443B4B06055BF90FD74B5794
Ruby 代码按预期工作。
对象代码:
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\u0026[^&]*" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *originalUrl = @"http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0&ms=au&expire=1368735912&id=e934f5f5c0743533&fexp=919374,909926,916713,916611,901474,924605,901208,929123,929915,929906,925714,929119,931202,928017,912518,911416,906906,904476,930807,919373,906836,933701,900345,926403,912711,929606,910075&sparams=cp,id,ip,ipbits,itag,ratebypass,source,upn,expire&sver=3&cp=U0hVTVdOU19GTENONV9PSFdKOnZFc0Uyc21YTVQw&ratebypass=yes&mv=m&source=youtube&itag=43&newshard=yes&mt=1368711866&ipbits=8&ip=92.114.198.83&key=yt1\\u0026quality=medium\\u0026type=video/webm&signature=AB8A6D618BDC38AF9D2E81916B863B724D2F12B6.8876CF4E106820B6443B4B06055BF90FD74B5794\\u0026fallback_host=tc.v19.cache7.c.youtube.com,url=http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0";
NSString *cleanUrl = [regex stringByReplacingMatchesInString:originalUrl options:0 range:NSMakeRange(0, [originalUrl length]) withTemplate:@"bla"];
注意 withTemplate:@"bla" 因为没有它我们看不到问题出在哪里。
执行:
clean_url = http://r6---sn-hvaquxaxjvh-3p8l.c.youtube.com/videoplayback?upn=StTvWU7n7N0blablablablablablablablablablablablablablablablabla
提前致谢!