1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 正则表达式提取html 中的网址 C#正则表达式模式从给定的字符串中提取网址 - 不是

正则表达式提取html 中的网址 C#正则表达式模式从给定的字符串中提取网址 - 不是

时间:2018-12-19 07:20:59

相关推荐

正则表达式提取html 中的网址 C#正则表达式模式从给定的字符串中提取网址 - 不是

正则表达式 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网址 也包括裸链接...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。