Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式获取字符串中的链接_C#_Regex - Fatal编程技术网

C# 使用正则表达式获取字符串中的链接

C# 使用正则表达式获取字符串中的链接,c#,regex,C#,Regex,我需要检查字符串是否包含链接并打印它。到目前为止,这是我的代码 string strRegex = @"(http://\w+\.\w+\.\w+)"; Regex myRegex = new Regex(strRegex, RegexOptions.None); string strTargetString = @"An http://w.facebook.me http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd ahttp vhttp b... ../

我需要检查字符串是否包含链接并打印它。到目前为止,这是我的代码

string strRegex = @"(http://\w+\.\w+\.\w+)";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"An http://w.facebook.me  http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd ahttp vhttp b... ../ .. extraordinary day dawns with each new day.";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    Console.WriteLine(myMatch.Value);
  }
}
输出:

http://w.facebook.me
http://www.w3.org
输出应该是

http://w.facebook.me
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd
在我的正则表达式中,我不想再添加一个
\w+
,因为我不知道链接中会有多少斜杠和多长单词

那么我怎样才能做得更好呢?在不知道链接的长度的情况下,如何获取链接

谢谢

将正则表达式更改为

"(http://\w+\.\w+\.\w+(\/\w+)*)"
如果你想匹配链接,比如在末尾加一个斜杠,只需将其改为

"(http://\w+\.\w+\.\w+(\/\w+)*\/?)"
将正则表达式更改为

"(http://\w+\.\w+\.\w+(\/\w+)*)"
如果你想匹配链接,比如在末尾加一个斜杠,只需将其改为

"(http://\w+\.\w+\.\w+(\/\w+)*\/?)"
regex only return我需要完整的链接regex only return我需要完整的链接