【发布时间】:2012-12-18 23:03:04
【问题描述】:
可能重复:
How to match URIs in text?
What is the best regular expression to check if a string is a valid URL?
我需要使用正则表达式 (PHP) 从包含许多 URL 的字符串中提取 zip 文件的 URL。
一个简单的例子应该会有所帮助:
目标:提取urlhttp://en.wikipedia.org/wiki/Kettle.zip
基本字符串:
/url?q=http://en.wikipedia.org/wiki/Kettle.zip&sa=U&ei=VpnIUP22Js blah /url?q=http://en.wikipedia.org/wiki/Kettle&sa=U&ei=VpnIUP22Js blah /url?q=http://en.wikipedia.org/wiki/Kettle.rar&sa=U&ei=VpnIUP22Js
更新; 假设基本字符串是
href="http://en.wikipedia.org/wiki/Kettle.zip">一些文字 /a>这里有一些其他文字 a href="http://google.com/wiki/Kettle"> /一个>
我需要提取 http://en.wikipedia.org/wiki/Kettle.zip
任何方法都可以...正则表达式与否。
【问题讨论】:
-
或其他许多谈论如何使该可点击的人之一,我敢打赌,在所有这些重复的 QA 材料中都有很多正则表达式。
-
你不能拆分
" blah ",解析URL查询字符串(和decodeURI!),获取q参数然后过滤.zip扩展? -
如果所有内容都是“url then description”的形式,用空格分隔,然后忘记正则表达式并使用拆分函数,您无需识别它是否是 url因为你有位置...... 然后你可以从查询字符串中解析url主机和路径。