C#regex模式从给定字符串提取URL-不是完整的html URL,而是裸链接

C#regex模式从给定字符串提取URL-不是完整的html URL,而是裸链接,c#,regex,url,hyperlink,extract,C#,Regex,Url,Hyperlink,Extract,我需要一个正则表达式,它将执行以下操作 Extract all strings which starts with http:// Extract all strings which starts with www. 所以我需要提取这2个 例如,下面有一个给定的字符串文本 house home go www.monstermmorpg.com nice hospital http://www.monstermmorpg.com this is incorrect url http://www.

我需要一个正则表达式,它将执行以下操作

Extract all strings which starts with http://
Extract all strings which starts with www.
所以我需要提取这2个

例如,下面有一个给定的字符串文本

house home go www.monstermmorpg.com nice hospital http://www.monstermmorpg.com this is incorrect url http://www.monstermmorpg.commerged continue
从上面给定的字符串中,我将得到

    www.monstermmorpg.com
http://www.monstermmorpg.com
http://www.monstermmorpg.commerged
寻找正则表达式或其他方法。多谢各位


C#4.0

您可以编写一些非常简单的正则表达式来处理这个问题,或者使用更传统的字符串拆分+LINQ方法

正则表达式 解释 模式:

基本上,该模式查找以
http://或https://或www.(?:https?://www\)
开头的字符串,然后将所有字符匹配到下一个空格

传统字符串选项
不适用于包含URL的html

例如,

<table><tr><td class="sub-img car-sm" rowspan ="1"><img src="https://{s3bucket}/abc/xyzxyzxyz/subject/jkljlk757cc617-a560-48f5-bea1-f7c066a24350_202008210836495252.jpg?X-Amz-Expires=1800&X-Amz-Algorithm=abcabcabc&X-Amz-Credential=AKIAVCAFR2PUOE4WV6ZX/20210107/ap-south-1/s3/aws4_request&X-Amz-Date=20210107T134049Z&X-Amz-SignedHeaders=host&X-Amz-Signature=3cc6301wrwersdf25fb13sdfcfe8c26d88ca1949e77d9e1d9af4bba126aa5fa91a308f7883e"></td><td class="icon"></td></tr></table>

最近出现了一些机器人程序,向我的游戏玩家发送URL。虽然我需要允许内部链接,但是也许你应该考虑不要使用正则表达式,因为这是解析HTML的一种笨拙的方法。如果要解析HTML字符串的一部分,则答案中的正则表达式不起作用。改为使用以下选项:
@“http(s)?:/([\w-]+\)+[\w-]+(/[\w-./?%&=]*)?“
正则表达式
@”\b(?:https?://www\)[^\f\n\r\t\v\]+\b”
工作得更好一些(在我的情况下),就好像URL包含在BB标记中一样,它将包含
]
,作为URL的一部分。@TomGullen Fair point。然而,方括号实际上是有效的URL字符(根据RFC规范),所以我将保留答案,因为这只是最一般的情况。
\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.
var 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);
<table><tr><td class="sub-img car-sm" rowspan ="1"><img src="https://{s3bucket}/abc/xyzxyzxyz/subject/jkljlk757cc617-a560-48f5-bea1-f7c066a24350_202008210836495252.jpg?X-Amz-Expires=1800&X-Amz-Algorithm=abcabcabc&X-Amz-Credential=AKIAVCAFR2PUOE4WV6ZX/20210107/ap-south-1/s3/aws4_request&X-Amz-Date=20210107T134049Z&X-Amz-SignedHeaders=host&X-Amz-Signature=3cc6301wrwersdf25fb13sdfcfe8c26d88ca1949e77d9e1d9af4bba126aa5fa91a308f7883e"></td><td class="icon"></td></tr></table>
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);