正则表达式以查找字符串中的URL

use*_*263 73 regex string url

有谁知道我可以用来查找字符串中的URL的正则表达式?我在Google上发现了很多正则表达式,用于确定整个字符串是否为URL,但我需要能够在整个字符串中搜索URL.例如,我希望能够找到www.google.comhttp://yahoo.com在以下字符串中:

Hello www.google.com World http://yahoo.com
Run Code Online (Sandbox Code Playgroud)

我不是在寻找字符串中的特定URL.我正在寻找字符串中的所有URL,这就是我需要正则表达式的原因.

Raj*_*eev 177

这是我使用的那个

(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?
Run Code Online (Sandbox Code Playgroud)

对我有用,也适合你.

  • 不要忘记逃避正斜杠。 (11认同)
  • Upvoted 但这个答案对问题所问的“www.yahoo.com”不起作用。`"""(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=% &:/~+#-]*[\w@?^=%&/~+#-])?""".r.findAllIn("www.google.com").toList` 。也缺乏对答案的解释 (5认同)
  • 不应该'[\ w _-]```[\ w-]`?因为`\ w`已匹配`_`.per [mozilla docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-word) (3认同)
  • 2017年了,unicode域名到处都是。`\w` 可能不匹配国际符号(取决于正则表达式引擎),而是需要范围:`a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF`。 (2认同)
  • 这对于一般用途来说很好,但是在很多情况下它并没有抓住。这将强制您的链接以协议为前缀。如果选择忽略协议,则像test@testing.com一样接受电子邮件结尾。 (2认同)
  • 我做了一个小小的更改,允许 http://、https:// 或 ftp:// 是可选的: `^((http|ftp|https)://)?([\w_-]+(?: (?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-] )?` (2认同)
  • 这里的问题是有人可以编写简写的 URL,例如“example.com” (2认同)

Ste*_*nze 38

猜猜没有正则表达式适合这种用途.我在这里发现了一个相当坚实的人

/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm
Run Code Online (Sandbox Code Playgroud)

与此处发布的其他差异相比,有些差异/优势:

  • 它与电子邮件地址匹配
  • 它确实匹配localhost:12345
  • 它不会检测到moo.com没有http或没有的东西www

请看这里的例子

  • 它匹配www.e这不是一个有效的网址 (4认同)
  • `g` 选项并非在所有正则表达式实现中都有效(例如 Ruby 的内置实现)。 (3认同)

Goo*_*JaY 14

text = """The link of this question: https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string
Also there are some urls: www.google.com, facebook.com, http://test.com/method?param=wasd
The code below catches all urls in text and returns urls in list."""

urls = re.findall('(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+', text)
print(urls)
Run Code Online (Sandbox Code Playgroud)

输出:

[
    'https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string', 
    'www.google.com', 
    'facebook.com',
    'http://test.com/method?param=wasd'
]
Run Code Online (Sandbox Code Playgroud)

  • url 中缺少“&”参数。例如 `http://test.com/method?param=wasd¶m2=wasd2` 缺少 param2 (2认同)

won*_*ngz 8

自己写了一篇:

let regex = /([\w+]+\:\/\/)?([\w\d-]+\.)*[\w-]+[\.\:]\w+([\/\?\=\&\#\.]?[\w-]+)*\/?/gm
Run Code Online (Sandbox Code Playgroud)

它适用于以下所有域:

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
Run Code Online (Sandbox Code Playgroud)

您可以在 regex101 上查看它的执行情况并根据需要进行调整

  • 它还匹配“alphanum_char.alphanum_char”形式的任何字符串,例如“ar”、“b.4”、“7.e”等。这些不是有效的 URL。 (7认同)
  • 不幸的是,这也匹配时间 - 09:00 (2认同)

Tom*_*zzo 7

如果您必须严格选择链接,我会选择:

(?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`!()\[\]{};:'".,<>?«»“”‘’]))
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请阅读以下内容:

用于匹配 URL 的改进的自由、准确的正则表达式模式

  • 不要那样做。http://www.regular-expressions.info/catastrophic.html 它会杀死你的应用程序...... (3认同)

Duc*_*lan 6

以上所有答案都不匹配 URL 中的 Unicode 字符,例如:http : //google.com?query=??c+filan+?ã+search

对于解决方案,这个应该有效:

(ftp:\/\/|www\.|https?:\/\/){1}[a-zA-Z0-9u00a1-\uffff0-]{2,}\.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S*)
Run Code Online (Sandbox Code Playgroud)

  • 根据 URL 上的 RFC 1738 (http://www.faqs.org/rfcs/rfc1738.html) 禁止使用 Unicode 字符。它们必须进行百分比编码以符合标准 - 尽管我认为它最近可能发生了变化 - 值得一读 https://www.w3.org/International/articles/idn-and-iri/ (2认同)

Squ*_*azz 6

这里提供的解决方案都没有解决我遇到的问题/用例.

我在这里提供的是迄今为止我发现/制作的最好的.当我发现它无法处理的新边缘情况时,我会更新它.

\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
(?<![.,?!-])
Run Code Online (Sandbox Code Playgroud)


小智 5

我使用下面的正则表达式在字符串中查找 url:

/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/
Run Code Online (Sandbox Code Playgroud)

  • `[a-zA-Z]{2,3}` 匹配TLD真的很差,见官方列表:https://data.iana.org/TLD/tlds-alpha-by-domain.txt (3认同)

Yus*_*sef 5

我认为这种正则表达式模式恰好可以满足您的需求

/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/
Run Code Online (Sandbox Code Playgroud)

这是提取Urls的摘要示例:

// 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  /sf/ask/422664301/ to filter goes here.";

// Check if there is a url in the text
preg_match_all($reg_exUrl, $text, $url,$matches);
var_dump($matches);
Run Code Online (Sandbox Code Playgroud)


小智 5

我发现涵盖了大多数示例链接,包括子目录部分。

正则表达式是:

(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?
Run Code Online (Sandbox Code Playgroud)


man*_*lds 3

如果您有 url 模式,您应该能够在字符串中搜索它。只需确保该模式没有^标记$url 字符串的开头和结尾即可。因此,如果 P 是 URL 的模式,则查找 P 的匹配项。