需求:
用戶端傳過來一段字串,需要從字串中匹配出所有的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。。。
正則一樣的,自己動動腦袋吧。