有没有人知道一个正则表达式可以用来在字符串中找到URL?我在Google上找到了很多用于确定整个字符串是否为URL的正则表达式,但我需要能够在整个字符串中搜索URL。例如,我想在以下字符串中找到www.google.com
和http://yahoo.com
:
Hello www.google.com World http://yahoo.com
我不是在寻找字符串中特定的URL地址,而是要找到字符串中的所有URL地址,因此我需要一个正则表达式。这是我使用的那一个
(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])
对我有效,也应该对你有效。
\w
可能无法匹配国际符号(取决于正则表达式引擎),因此需要使用范围:a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF
。 - Michael Antipin我想说没有一种正则表达式能完美地解决这个问题。我找到了一个相当可靠的正则表达式在这里
。/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm
与此处发布的其他内容相比,一些差异/优势:
http
或 www
,它不会检测到像 moo.com
这样的东西请参阅此处的示例
g
选项并非所有正则表达式实现(例如Ruby内置实现)都支持。 - Huliaxtext = """The link of this question: https://dev59.com/rm025IYBdhLWcg3wYE-G
Also there are some urls: www.google.com, facebook.com, http://test.com/method?param=wasd, http://test.com/method?param=wasd¶ms2=kjhdkjshd
The code below catches all urls in text and returns urls in list."""
urls = re.findall('(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-&?=%.]+', text)
print(urls)
输出:
[
'https://dev59.com/rm025IYBdhLWcg3wYE-G',
'www.google.com',
'facebook.com',
'http://test.com/method?param=wasd',
'http://test.com/method?param=wasd¶ms2=kjhdkjshd'
]
&
参数。例如,http://test.com/method?param=wasd¶m2=wasd2
缺少 param2。 - TrophyGeek&
。正确的版本应该是:val urlRegex = "(?:(?:https?|ftp):\\/\\/)?[\\w/\\-?=%.]+\\.[\\w/\\-&?=%.]+"
。 - Alechello...
是一个URL。 - mathematics-and-caffeine我自己写了一个:
let regex = /([\w+]+\:\/\/)?([\w\d-]+\.)*[\w-]+[\.\:]\w+([\/\?\=\&\#\.]?[\w-]+)*\/?/gm
它适用于以下所有域名:
https://www.facebook.com
https://app-1.number123.com
http://facebook.com
ftp://facebook.com
http://localhost:3000
localhost:3000/
unitedkingdomurl.co.uk
this.is.a.url.com/its/still=going?wow
shop.facebook.org
app.number123.com
app1.number123.com
app-1.numbEr123.com
app.dashes-dash.com
www.facebook.com
facebook.com
fb.com/hello_123
fb.com/hel-lo
fb.com/hello/goodbye
fb.com/hello/goodbye?okay
fb.com/hello/goodbye?okay=alright
Hello www.google.com World http://yahoo.com
https://www.google.com.tr/admin/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
https://google.com.tr/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
http://google.com/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
ftp://google.com/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
www.google.com.tr/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
www.google.com/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
drive.google.com/test/subPage?qs1=sss1&qs2=sss2&qs3=sss3#Services
https://www.example.pl
http://www.example.com
www.example.pl
example.com
http://blog.example.com
http://www.example.com/product
http://www.example.com/products?id=1&page=2
http://www.example.com#up
http://255.255.255.255
255.255.255.255
shop.facebook.org/derf.html
您可以在regex101上查看它的性能,并根据需要进行调整。
字母数字字符.字母数字字符
的任何字符串,例如 a.r
、b.4
、7.e
等。它们并不是有效的 URL。 - Princy这里提供的解决方案都没有解决我所遇到的问题或使用情况。
我在这里提供的是目前为止我找到/制作的最佳解决方案。如果我发现它无法处理新的边缘情况,我会进行更新。
\b
#Word cannot begin with special characters
(?<![@.,%&#-])
#Protocols are optional, but take them with us if they are present
(?<protocol>\w{2,10}:\/\/)?
#Domains have to be of a length of 1 chars or greater
((?:\w|\&\#\d{1,5};)[.-]?)+
#The domain ending has to be between 2 to 15 characters
(\.([a-z]{2,15})
#If no domain ending we want a port, only if a protocol is specified
|(?(protocol)(?:\:\d{1,6})|(?!)))
\b
#Word cannot end with @ (made to catch emails)
(?![@])
#We accept any number of slugs, given we have a char after the slash
(\/)?
#If we have endings like ?=fds include the ending
(?:([\w\d\?\-=#:%@&.;])+(?:\/(?:([\w\d\?\-=#:%@&;.])+))*)?
#The last char cannot be one of these symbols .,?!,- exclude these
(?<![.,?!-])
/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/
这是一个提取URL的代码示例:
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// The Text you want to filter for urls
$text = "The text you want https://dev59.com/rm025IYBdhLWcg3wYE-G to filter goes here.";
// Check if there is a url in the text
preg_match_all($reg_exUrl, $text, $url,$matches);
var_dump($matches);
如果你在选择链接时必须严格要求,我会选择:
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
更多信息请阅读以下内容:
(ftp:\/\/|www\.|https?:\/\/){1}[a-zA-Z0-9u00a1-\uffff0-]{2,}\.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S*)
我使用以下正则表达式在字符串中查找URL:
/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/
[a-zA-Z]{2,3}
对于匹配顶级域名来说实在太粗糙了,请参考官方列表:https://data.iana.org/TLD/tlds-alpha-by-domain.txt。 - Toto我找到了这个,其中包含大多数示例链接,包括子目录部分。
正则表达式为:
(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?
preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match);
来自 https://dev59.com/cXNA5IYBdhLWcg3wkuzO - Avatar