Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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# 如何使用正则表达式查找包含url的文本?_C#_Regex - Fatal编程技术网

C# 如何使用正则表达式查找包含url的文本?

C# 如何使用正则表达式查找包含url的文本?,c#,regex,C#,Regex,如果传入的文本参数包含url,我将尝试创建一个返回true的方法。以下是我到目前为止的情况: private bool TextContainsUrl(string text) { Regex rgx = new Regex(@"((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?"); bool match = rgx.IsMatc

如果传入的文本参数包含url,我将尝试创建一个返回true的方法。以下是我到目前为止的情况:

private bool TextContainsUrl(string text)
{
  Regex rgx = new Regex(@"((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?");
  bool match = rgx.IsMatch(text);

  return match;
}
我可以这样称呼它:

TextContainsUrl("here is a text with url http://something.net bla bla.");

问题是,上面的两个调用都返回true


我做错了什么?

去掉
[\w+?\.\w+]
周围的方括号,你就应该很好了。方括号内的字符按任何顺序匹配


请在此处尝试:

没有以
www://
开头的URI方案。有关当前注册的所有URI方案,请参见此处:
TextContainsUrl("here is a text with no url bla bla.");