Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# 正则表达式:来自文本的URI部分_C#_.net_Regex_Uri - Fatal编程技术网

C# 正则表达式:来自文本的URI部分

C# 正则表达式:来自文本的URI部分,c#,.net,regex,uri,C#,.net,Regex,Uri,全部!我有一个这样的文本“一些带有uri和其他单词的文本”。我需要使用one正则表达式获取部分uri 我试试这个: string text = "Some text with uri http://test.com and other words."; string pattern = @"\b(\S+)://([^:]+)(?::(\S+))?\b"; MatchCollection matches = Regex.Matches(text, pattern); 当我写“带uri的文本”

全部!我有一个这样的文本“一些带有uri和其他单词的文本”。我需要使用one正则表达式获取部分uri

我试试这个:

string text = "Some text with uri http://test.com and other words.";
string pattern = @"\b(\S+)://([^:]+)(?::(\S+))?\b"; 
MatchCollection matches = Regex.Matches(text, pattern); 
当我写“带uri的文本”或“word1-word2”时,它是有效的


错误在哪里

这会让你更接近。。。我还是不确定你想要什么

如果你能展示你想要的结果,这会有帮助

\b(\S+)://([^: ]+)

您的第二个
+
修饰符是贪婪的,因此它匹配
http://
之后的所有内容,除非它碰到
或行尾。请尝试以下操作:

@"\b(\w+)://([^:]+?)(?::(\S+))?\b"

什么不起作用?你说“它起作用”。我猜这是返回
http://test.com 还有其他的话。
是这样吗?