[英]java regular expressions matching specific urls
我曾經在php中構建了一個程序,它使用非常特定的正則表達式來匹配鏈接,但是這種模式似乎不適用於java,我試圖找到相當於java的java
"~http://(bit.ly|t.co)~"
在PHP中,這將匹配http://t.co/UURRNlrK和http://bit.ly/AenG5W這樣的鏈接,這將是一個java的等價物嗎?
http://(bit\.ly|t\.co)/\w*
我認為這個結果與上限相同
我想你在找
String str = "http://t.co/UURRNlrK";
String p = "(http://(t\.co|bit\.ly).*)";
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(str);
if(matcher.find())
System.out.println(matcher.group(0));
輸出= http://t.co/UURRNlrK
如果str = "http://bit.ly/AenG5W"
輸出= http://bit.ly/AenG5W
這是一個很好的Java正則教程 。
我試過這個:
String str = "http://bit.ly/asdfsd";
if(str.matches("http://(bit\.ly|t\.co).+")){
System.out.println("hurray");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.