【问题标题】:regex to extract URLs from text - Ruby正则表达式从文本中提取 URL - Ruby
【发布时间】:2019-09-14 00:35:57
【问题描述】:

我正在尝试从文本中检测 urls 并通过用引号括起来来替换它们,如下所示:

original text: Hey, it is a url here www.example.com
required text: Hey, it is a url here "www.example.com"

original text 显示我的输入值,required text 表示所需的输出。我在网上搜索了很多,但找不到任何可能的解决方案。我已经尝试过URL.extract 功能,但如果没有httphttps,它似乎无法检测到URLs。以下是我要处理的一些 url 的示例。如果您知道解决方案,请告诉我。

ANQUETIL-DUPERRON Abraham-Hyacinthe,KIEFFER Jean-Luc,www.hominides.net/html/actualites/outils-preuve-presence-hominides-asie-0422.php,Les Belles lettres,2001 年。

https://www.ancient-code.com/indian-archeologists-stumbleacross-ruins-great-forgotten-civilization-mizoram/

www.jstor.org/stable/24084454

www.biorespire.com/2016/03/22/une-nouvelle-villeantique-d%C3%A9couverte-en-inde/

insu.cnrs.fr/terre-solide/terre-et-vie/de-nouvellesdatations-repoussent-l-age-de-l-apparition-d-outils-surle-so

www.cerege.fr/spip.php?page=pageperso&id_user=94

【问题讨论】:

  • 那么你对这个任何有domain.root_domain的字符串的逻辑是什么?
  • @lacostenycoder 请检查更新的文本。谢谢
  • 匹配任意 URL 并不容易,如果你还想匹配googl.com 之类的字符串,你肯定会得到误报。请查看this thread,了解哪种模式最适合您的场景。一旦你知道哪种模式好用,剩下的就很容易了。

标签: ruby-on-rails regex ruby ruby-on-rails-4


【解决方案1】:

查找看起来像 url 的单词:

str = "ANQUETIL-DUPERRON Abraham-Hyacinthe, KIEFFER Jean-Luc, www.hominides.net/html/actualites/outils-preuve-presence-hominides-asie-0422.php,Les Belles lettres, 2001.\n\nhttps://www.ancient-code.com/indian-archeologists-stumbleacross-ruins-great-forgotten-civilization-mizoram/\n\nwww.jstor.org/stable/24084454\n\nwww.biorespire.com/2016/03/22/une-nouvelle-villeantique-d%C3%A9couverte-en-inde/\n\ninsu.cnrs.fr/terre-solide/terre-et-vie/de-nouvellesdatations-repoussent-l-age-de-l-apparition-d-outils-surle-so\n\nwww.cerege.fr/spip.php?page=pageperso&id_user=94"

str.split.select{|w| w[/(\b+\.\w+)/]}

这将为您提供一组没有空格并包含一个或多个 . 字符的单词,这些字符可能适用于您的用例。

puts str.split.select{|w| w[/(\b+\.\w+)/]}
www.hominides.net/html/actualites/outils-preuve-presence-hominides-asie-0422.php,
https://www.ancient-code.com/indian-archeologists-stumbleacross-ruins-great-forgotten-civilization-mizoram/
www.jstor.org/stable/24084454
www.biorespire.com/2016/03/22/une-nouvelle-villeantique-d%C3%A9couverte-en-inde/
insu.cnrs.fr/terre-solide/terre-et-vie/de-nouvellesdatations-repoussent-l-age-de-l-apparition-d-outils-surle-so
www.cerege.fr/spip.php?page=pageperso&id_user=94

更新

修改字符串的完整解决方案:

str_with_quote = str.clone # make a clone for the `gsub!`

str.split.select{|w| w[/(\b+\.\w+)/]}
   .each{|url| str_with_quote.gsub!(url, '"' + url + '"')} 

现在您的克隆对象将 url 包含在双引号中

puts str_with_quote

会给你这个输出

ANQUETIL-DUPERRON Abraham-Hyacinthe, KIEFFER Jean-Luc, "www.hominides.net/html/actualites/outils-preuve-presence-hominides-asie-0422.php,Les" Belles lettres, 2001.

"https://www.ancient-code.com/indian-archeologists-stumbleacross-ruins-great-forgotten-civilization-mizoram/"

"www.jstor.org/stable/24084454"

"www.biorespire.com/2016/03/22/une-nouvelle-villeantique-d%C3%A9couverte-en-inde/"

"insu.cnrs.fr/terre-solide/terre-et-vie/de-nouvellesdatations-repoussent-l-age-de-l-apparition-d-outils-surle-so"

"www.cerege.fr/spip.php?page=pageperso&id_user=94"

【讨论】:

  • 经典修复。你震撼了
  • 我希望我能给你超过 1 个赞
  • @ImranNaqvi 都很好,请查看更新的答案,我认为这是完整的解决方案。
猜你喜欢
  • 2017-10-08
  • 1970-01-01
  • 2021-02-05
  • 2015-03-10
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多