我正在尝试从字符串中提取一些不完整的URL。让我举一个例子来说明我所说的不完全URL的含义:
tny.sh/FJFCG8w
gka.co/cte3
google.com
cdn.ne/ecoe3我已经检查了一些使用regex来检测前缀的解决方案,比如HTTP之类的。但是上面提到的链接是没有前缀的链接.那么它有可能这样做吗?
这是我尝试用它在字符串中提取URL的方法:
protected LinkedList<string> ExtractLink(string txt)
{
var linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
LinkedList<string> urls = new LinkedList<string>();
foreach (Match m in linkParser.Matches(txt))
urls.AddFirst(m.Value);
return urls;
}这是调用该方法的一个示例:
ExtractLink("Hello, this is the link that you need to check tny.sh/FJFCG8w");发布于 2022-06-16 18:41:59
您可以使用这个regex代替
[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}([-a-zA-Z0-9@:%_\+.~#?&\/=])*如果您还想将urls与http(s)协议匹配,请使用以下命令
(https?:\/\/)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}([-a-zA-Z0-9@:%_\+.~#?&\/=])*https://stackoverflow.com/questions/72617539
复制相似问题