如何从NSString中的句子中提取URL? [英] How can I extract a URL from a sentence that is in a NSString?

查看:217
本文介绍了如何从NSString中的句子中提取URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要完成的工作如下。我有一个NSString的一个句子在感情中有一个URL。我需要能够抓住在NSString内的任何句子中呈现的URL,例如:

What I'm trying to accomplish is as follows. I have a NSString with a sentence that has a URL within the sentience. I'm needing to be able to grab the URL that is presented within any sentence that is within a NSString so for example:

假设我有这个NSString

Let's say I had this NSString

NSString *someString = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it.";

我需要能够提取 http://abc.com/efg.php?EFAei687e3EsA 从该NSString。这个NSString不是静态的,并且将改变结构,并且url不一定在句子的同一位置。我试图查看three20代码,但它对我没有意义。如何做到这一点呢?感谢您的帮助。

I need to be able to extract http://abc.com/efg.php?EFAei687e3EsA from within that NSString. This NSString isn't static and will be changing structure and the url will not necessarily be in the same spot of the sentence. I've tried to look into the three20 code but it makes no sense to me. How else can this be done? Thanks for help.

推荐答案

编辑:我将在这里出去说你应该使用 NSDataDetector 作为Dave提及。

I'm going to go out on a limb here and say you should probably use NSDataDetector as Dave mentions. Far less prone to error than regular expressions.

请参阅正则表达式。您可以使用 NSRegularExpression 来创建一个简单的URL, a>类(仅适用于iPhone,但由于这是你的目标,你没关系),或在网上找到一个你可以使用。有关使用类的教程,请参见此处

Take a look at regular expressions. You can construct a simple one to extract the URL using the NSRegularExpression class (only available on iPhone, but since that's what you're targeting, you're ok), or find one online that you can use. For a tutorial on using the class, see here.

您希望的代码实际上看起来像这样(使用John Gruber的 super URL regex ):

The code you want essentially looks like this (using John Gruber's super URL regex):

NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»""‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];
NSString *someString = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it.";
NSString *match = [someString substringWithRange:[expression rangeOfFirstMatchInString:someString options:NSMatchingCompleted range:NSMakeRange(0, [someString length])]];
NSLog(@"%@", match); // Correctly prints 'http://abc.com/efg.php?EFAei687e3EsA'

这将提取任何字符串中的第一个URL(当然,这没有错误检查,所以如果字符串真的不包含任何URL,它将无法工作,但可以看一下 NSRegularExpression 类,看看如何解决它。

That will extract the first URL in any string (of course, this does no error checking, so if the string really doesn't contain any URL's it won't work, but take a look at the NSRegularExpression class to see how to get around it.

这篇关于如何从NSString中的句子中提取URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆