use*_*452 1 url range nsstring ios
我试图从一串文本中提取图像的URL.它可能来自不同的网站,可能是任何形式的图像(jpg,png,gif等).什么是扫描字符串,找到任何匹配的图片扩展,并从中获取图形的好方法?
字符串可能是这样的:
Hello, I hope you like my picture located at http://www.website.com/picture1.png. However, if you don't, I know you'll like this one http://www.website2.org/picture2.jpg.Please refer all comments to http://www.website5.com/
Run Code Online (Sandbox Code Playgroud)
我希望能够扫描字符串以查找图像文件的唯一URL,并创建一个新的字符串,无论第一个图像URL是什么.所以在这个字符串中,我可以创建一个只有http://www.website.com/picture1.png的新字符串
小智 5
我来自The Link Objective-C - 在字符串中查找URL
不需要使用RegexKitLite,因为iOS 4 Apple提供NSDataDetector(子类NSRegularExpression).
您可以像这样使用它(source是您的字符串):
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray* matches = [detector matchesInString:source options:0 range:NSMakeRange(0, [source length])];
Run Code Online (Sandbox Code Playgroud)