gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:06:19 28 4
gpt4 key购买 nike

全部!我有这样的文本“一些带有 uri http://test.com 和其他词的文本。”。我需要使用 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);

当我写“Some text with uri http://test.com”或“word1 http://test.com:5000 word2”时,它起作用了。

错在哪里?

最佳答案

您的第二个 + 修饰符是贪婪的,因此它匹配 http:// 之后的所有内容,除非它命中 : 或结尾线。试试这个:

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

关于c# - 正则表达式:来自文本的 URI 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975675/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com