[英]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#.我有一个字符串,例如“转到堆栈溢出http://stackoverflow.com ”现在我想使用 c# 解析该字符串中的链接。 I wants the output in the following format.
我想要以下格式的 output。 "
Go to stack overflow <a href=http://stackoverflow.com target=_blank>http://stackoverflow.com</>
" "
Go to stack overflow <a href=http://stackoverflow.com target=_blank>http://stackoverflow.com</>
"
Is it possible in C#? 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]\,[az]{2?6})(?[0-9]{1.4});((/?)|(/[0-9a-z_:*'(),??:@&=$,%#-])/?)$
使用具有此模式的正则表达式
^(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]\,[az]{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: Output:
Go to stack overflow Go to stack overflow <a href=http://stackoverflow.com target=_blank>http://stackoverflow.com</>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.