开发者

How to parse links from the string

开发者 https://www.devze.com 2023-03-16 21:54 出处:网络
I have a string like \"Go to stack overflow http://stackoverflow.com\" Now I want 开发者_开发知识库to parse link from this string using c#.

I have a string like "Go to stack overflow http://stackoverflow.com" Now I want 开发者_开发知识库to parse link from this string using c#. I wants the output in the following format. "Go to stack overflow <a href=http://stackoverflow.com target=_blank>http://stackoverflow.com</>"

Is it possible in C#?


Use regular expression with this pattern ^(https?://)?(([0-9a-z_!'().&=$%-]: )?[0-9a-z_!'().&=$%-]@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!'()-]\.)([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!*'().;?:@&=$,%#-])/?)$


var input = @"Go to stack overflow http://stackoverflow.com";
var result = Regex.Replace(input, 
    @"((?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*)",
    @"Go to stack overflow <a href=$1 target=_blank>$1</>");

Output:

Go to stack overflow Go to stack overflow <a href=http://stackoverflow.com target=_blank>http://stackoverflow.com</>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号