【发布时间】: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 功能,但如果没有http 或https,它似乎无法检测到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 年。
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