您好,欢迎访问一九零五行业门户网

正则表达式 - PHP 字符串中匹配url

需求:
客户端传过来一段字符串,需要从字符串中匹配出所有的url,包括域名或ip后面的参数(含端口)

url样例:
http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23或者http://www.baidu.com/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

当然简单url也是要匹配出来的
求解正则
回复内容: 需求:
客户端传过来一段字符串,需要从字符串中匹配出所有的url,包括域名或ip后面的参数(含端口)

url样例:
http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23或者http://www.baidu.com/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23

当然简单url也是要匹配出来的
求解正则
先用比较宽泛的正则匹配出所有的url,例如
https?:\/\/\s+
然后对于这堆url依次采用parse_url函数
^(http|https|ftp)\://[a-za-z0-9\-\.]+\.[a-za-z]{2,3}(:[a-za-z0-9]*)?/?([a-za-z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$http://regexlib.com/search.aspx?k=url&c=-1&m=5&ps=20
java 大概这么写
string str = 接收到的字符串string regex = (http:|https:)//[^[a-za-z0-9,:\\._\\?%&+\\-=/#]]*; pattern pattern = pattern.compile(regex); matcher matcher = pattern.matcher(str); while (matcher.find()) { string url=matcher.group(); system.out.println(url); }
以下字符串通过测试.
string str=http://127.0.0.1:6666/ + https://www.baidu.com/ + http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23\n + 或者\n + 哈哈http://www.baidu.com:85676/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23 6666都是对的;
输出
http://127.0.0.1:6666/https://www.baidu.com/http://127.0.0.1/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23http://www.baidu.com:85676/metinfo/img/img.php?class1=1&serch_sql=%201=if%28ascii%28substr%28user%28%29,1,1%29%29=114,1,2%29%23
什么,你问的式php?抱歉,我不会php。。。
正则一样的,自己动动脑袋吧。
其它类似信息

推荐信息