How to parse links from the string
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</>
精彩评论