正则表达式 H3>
var linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", piled | RegexOptions.IgnoreCase);
var rawString = "house home go nice hospital this is incorrect url merged continue";
foreach(Match m in linkParser.Matches(rawString))
MessageBox.Show(m.Value);说明
模式:
\b -matches a word boundary (spaces, periods..etc)
(?: -define the beginning of a group, the ?: specifies not to capture the data within this group.
https?:// - Match http or https (the '?' after the "s" makes it optional)
| -OR
www\. -literal string, match www. (the \. means a literal ".")
) -end group
\S+ -match a series of non-whitespace characters.
\b -match the closing word boundary.基本上,该模式查找以http:// OR https:// OR www. (?:https?://|www\.)开头的字符串,然后匹配所有字符直到下一个空格。
传统字符串选项 h3>
var rawString = "house home go nice hospital this is incorrect url merged 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);
正则表达式提取html 中的网址 C#正则表达式模式从给定的字符串中提取网址 - 不是完整的html网址 也包括裸链接...