在字符串中查找 URL 的正则表达式 [英] Regular expression to find URLs within a string
问题描述
有谁知道我可以用来在字符串中查找 URL 的正则表达式吗?我在 Google 上发现了很多正则表达式,用于确定整个字符串是否为 URL,但我需要能够搜索整个字符串以查找 URL.例如,我希望能够在以下字符串中找到 www.google.com
和 http://yahoo.com
:
Does anyone know of a regular expression I could use to find URLs within a string? I've found a lot of regular expressions on Google for determining if an entire string is a URL but I need to be able to search an entire string for URLs. For example, I would like to be able to find www.google.com
and http://yahoo.com
in the following string:
Hello www.google.com World http://yahoo.com
我不是在字符串中寻找特定的 URL.我正在寻找字符串中的所有 URL,这就是我需要正则表达式的原因.
I am not looking for specific URLs in the string. I am looking for ALL of the URLs in the string which is why I need a regular expression.
推荐答案
这个是我用的
(http|ftp|https)://([w_-]+(?:(?:.[w_-]+)+))([w.,@?^=%&:/~+#-]*[w@?^=%&/~+#-])
对我有用,对你也应该有用.
Works for me, should work for you too.
这篇关于在字符串中查找 URL 的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!