I am consuming the Twitter API and want to convert all URLs to hyperlinks.
我正在使用Twitter API,并希望将所有url转换为超链接。
What is the most effective way you've come up with to do this?
你想出的最有效的方法是什么?
from
从
string myString = "This is my tweet check it out http://tinyurl.com/blah";
to
来
This is my tweet check it out <a href="http://tinyurl.com/blah">http://tinyurl.com/>blah</a>
5 个解决方案
#1
22
Regular expressions are probably your friend for this kind of task:
正则表达式可能是这类任务的朋友:
Regex r = new Regex(@"(https?://[^\s]+)");
myString = r.Replace(myString, "<a href=\"$1\">$1</a>");
The regular expression for matching URLs might need a bit of work.
匹配url的正则表达式可能需要做一些工作。
#2
7
I did this exact same thing with jquery consuming the JSON API here is the linkify function:
我在jquery使用JSON API时做了同样的事情这是linkify函数:
String.prototype.linkify = function() {
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
return m.link(m);
});
};
#3
5
This is actually an ugly problem. URLs can contain (and end with) punctuation, so it can be difficult to determine where a URL actually ends, when it's embedded in normal text. For example:
这实际上是一个丑陋的问题。URL可以包含标点(并以标点结尾),因此当URL嵌入到普通文本中时,很难确定它实际上在哪里结束。例如:
http://example.com/.
is a valid URL, but it could just as easily be the end of a sentence:
是一个有效的URL,但它也可以很容易地成为一句话的结尾:
I buy all my witty T-shirts from http://example.com/.
You can't simply parse until a space is found, because then you'll keep the period as part of the URL. You also can't simply parse until a period or a space is found, because periods are extremely common in URLs.
您不能简单地解析,直到找到一个空格,因为这样您将把句点作为URL的一部分。您也不能简单地解析一个句点或找到一个空格,因为句点在url中非常常见。
Yes, regex is your friend here, but constructing the appropriate regex is the hard part.
是的,regex是您的朋友,但是构建适当的regex是困难的部分。
Check out this as well: Expanding URLs with Regex in .NET.
也可以看看这个:在。net中使用Regex扩展url。
#4
1
You can add some more control on this by using MatchEvaluator delegate function with regular expression: suppose i have this string:
通过使用MatchEvaluator委托函数并使用正则表达式,可以在上面添加更多的控件:假设我有这个字符串:
find more on http://www.*.com
now try this code
现在试试这个代码
private void ModifyString()
{
string input = "find more on http://www.authorcode.com ";
Regex regx = new Regex(@"\b((http|https|ftp|mailto)://)?(www.)+[\w-]+(/[\w- ./?%&=]*)?");
string result = regx.Replace(input, new MatchEvaluator(ReplaceURl));
}
static string ReplaceURl(Match m)
{
string x = m.ToString();
x = "< a href=\"" + x + "\">" + x + "</a>";
return x;
}
#5
1
/cheer for RedWolves
/为RedWolves
from: this.replace(/[A-Za-z]+://[A-Za-z0-9-]+.[A-Za-z0-9-:%&\?/.=]+/, function(m){...
来自:this.replace(/[A-Za-z]+:/ /[A-Za-z0-9 -]+。[A-Za-z0-9 -:% & \ ? /。=]+ /函数(m){…
see: /[A-Za-z]+://[A-Za-z0-9-]+.[A-Za-z0-9-:%&\?/.=]+/
见:[A-Za-z]+:/ /[A-Za-z0-9 -]+。[A-Za-z0-9 -:% & \ ? / =]。+ /
There's the code for the addresses "anyprotocol"://"anysubdomain/domain"."anydomainextension and address",
这是地址“anyprotocol”://“anysubdomain/domain”的代码。anydomainextension和地址”,
and it's a perfect example for other uses of string manipulation. you can slice and dice at will with .replace and insert proper "a href"s where needed.
这是字符串处理的一个很好的例子。你可以用.replace随意切片,并在需要的地方插入适当的“a href”。
I used jQuery to change the attributes of these links to "target=_blank" easily in my content-loading logic even though the .link method doesn't let you customize them.
我使用jQuery在我的内容加载逻辑中很容易地将这些链接的属性更改为“target=_blank”,尽管.link方法不允许您自定义它们。
I personally love tacking on a custom method to the string object for on the fly string-filtering (the String.prototype.linkify declaration), but I'm not sure how that would play out in a large-scale environment where you'd have to organize 10+ custom linkify-like functions. I think you'd definitely have to do something else with your code structure at that point.
我个人喜欢在动态字符串过滤(string .prototype)中为字符串对象添加自定义方法。但是我不确定在大型环境中如何实现这一点,在这种环境中,您必须组织10多个自定义的类似于linkify的函数。我认为此时您肯定需要对代码结构做一些其他的事情。
Maybe a vet will stumble along here and enlighten us.
也许一个兽医会在这里蹒跚而行,启发我们。
#1
22
Regular expressions are probably your friend for this kind of task:
正则表达式可能是这类任务的朋友:
Regex r = new Regex(@"(https?://[^\s]+)");
myString = r.Replace(myString, "<a href=\"$1\">$1</a>");
The regular expression for matching URLs might need a bit of work.
匹配url的正则表达式可能需要做一些工作。
#2
7
I did this exact same thing with jquery consuming the JSON API here is the linkify function:
我在jquery使用JSON API时做了同样的事情这是linkify函数:
String.prototype.linkify = function() {
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
return m.link(m);
});
};
#3
5
This is actually an ugly problem. URLs can contain (and end with) punctuation, so it can be difficult to determine where a URL actually ends, when it's embedded in normal text. For example:
这实际上是一个丑陋的问题。URL可以包含标点(并以标点结尾),因此当URL嵌入到普通文本中时,很难确定它实际上在哪里结束。例如:
http://example.com/.
is a valid URL, but it could just as easily be the end of a sentence:
是一个有效的URL,但它也可以很容易地成为一句话的结尾:
I buy all my witty T-shirts from http://example.com/.
You can't simply parse until a space is found, because then you'll keep the period as part of the URL. You also can't simply parse until a period or a space is found, because periods are extremely common in URLs.
您不能简单地解析,直到找到一个空格,因为这样您将把句点作为URL的一部分。您也不能简单地解析一个句点或找到一个空格,因为句点在url中非常常见。
Yes, regex is your friend here, but constructing the appropriate regex is the hard part.
是的,regex是您的朋友,但是构建适当的regex是困难的部分。
Check out this as well: Expanding URLs with Regex in .NET.
也可以看看这个:在。net中使用Regex扩展url。
#4
1
You can add some more control on this by using MatchEvaluator delegate function with regular expression: suppose i have this string:
通过使用MatchEvaluator委托函数并使用正则表达式,可以在上面添加更多的控件:假设我有这个字符串:
find more on http://www.*.com
now try this code
现在试试这个代码
private void ModifyString()
{
string input = "find more on http://www.authorcode.com ";
Regex regx = new Regex(@"\b((http|https|ftp|mailto)://)?(www.)+[\w-]+(/[\w- ./?%&=]*)?");
string result = regx.Replace(input, new MatchEvaluator(ReplaceURl));
}
static string ReplaceURl(Match m)
{
string x = m.ToString();
x = "< a href=\"" + x + "\">" + x + "</a>";
return x;
}
#5
1
/cheer for RedWolves
/为RedWolves
from: this.replace(/[A-Za-z]+://[A-Za-z0-9-]+.[A-Za-z0-9-:%&\?/.=]+/, function(m){...
来自:this.replace(/[A-Za-z]+:/ /[A-Za-z0-9 -]+。[A-Za-z0-9 -:% & \ ? /。=]+ /函数(m){…
see: /[A-Za-z]+://[A-Za-z0-9-]+.[A-Za-z0-9-:%&\?/.=]+/
见:[A-Za-z]+:/ /[A-Za-z0-9 -]+。[A-Za-z0-9 -:% & \ ? / =]。+ /
There's the code for the addresses "anyprotocol"://"anysubdomain/domain"."anydomainextension and address",
这是地址“anyprotocol”://“anysubdomain/domain”的代码。anydomainextension和地址”,
and it's a perfect example for other uses of string manipulation. you can slice and dice at will with .replace and insert proper "a href"s where needed.
这是字符串处理的一个很好的例子。你可以用.replace随意切片,并在需要的地方插入适当的“a href”。
I used jQuery to change the attributes of these links to "target=_blank" easily in my content-loading logic even though the .link method doesn't let you customize them.
我使用jQuery在我的内容加载逻辑中很容易地将这些链接的属性更改为“target=_blank”,尽管.link方法不允许您自定义它们。
I personally love tacking on a custom method to the string object for on the fly string-filtering (the String.prototype.linkify declaration), but I'm not sure how that would play out in a large-scale environment where you'd have to organize 10+ custom linkify-like functions. I think you'd definitely have to do something else with your code structure at that point.
我个人喜欢在动态字符串过滤(string .prototype)中为字符串对象添加自定义方法。但是我不确定在大型环境中如何实现这一点,在这种环境中,您必须组织10多个自定义的类似于linkify的函数。我认为此时您肯定需要对代码结构做一些其他的事情。
Maybe a vet will stumble along here and enlighten us.
也许一个兽医会在这里蹒跚而行,启发我们。