简体   繁体   中英

java regular expressions matching specific urls

I once built a program in php that used very specific regular expressions to match links, however that pattern doesnt seem to work in java, Im trying to find the java equivalent of

"~http://(bit.ly|t.co)~"

in php this would would match links such as http://t.co/UURRNlrK and http://bit.ly/AenG5W what would be a java equivalent of this?

http://(bit\.ly|t\.co)/\w*

我认为这个结果与上限相同

I think you are looking for

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));

Output = http://t.co/UURRNlrK

if str = "http://bit.ly/AenG5W"

Output = http://bit.ly/AenG5W

Here is a nice Regex Tutorial for java.

I tried this:

String str = "http://bit.ly/asdfsd";

if(str.matches("http://(bit\.ly|t\.co).+")){
    System.out.println("hurray");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM