【发布时间】:2016-12-11 23:33:47
【问题描述】:
阅读此问题的答案后:C# regex pattern to extract urls from given string - not full html urls but bare links as well 我想知道哪种方法是从文档中提取 url 的最快方法,使用正则表达式匹配或使用字符串拆分方法。
所以,您有一个包含 html 文档的字符串,并且想要提取 url。
正则表达式的方式是:
Regex linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string rawString = "house home go www.monstermmorpg.com nice hospital http://www.monstermmorpg.com this is incorrect url http://www.monstermmorpg.commerged continue";
foreach(Match m in linkParser.Matches(rawString))
MessageBox.Show(m.Value);
以及字符串拆分方法:
string rawString = "house home go www.monstermmorpg.com nice hospital http://www.monstermmorpg.com this is incorrect url http://www.monstermmorpg.commerged continue";
var links = rawString.Split("\t\n ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Where(s => s.StartsWith("http://") || s.StartsWith("www.") || s.StartsWith("https://"));
foreach (string s in links)
MessageBox.Show(s);
哪种方式最高效?
【问题讨论】:
-
你可以同时用秒表试试
-
我很惭愧地承认我的第一个想法是“秒表是某种基准程序”
-
我无法进行基准测试,因为我已经有几天无法使用 PC。