简体   繁体   English

java正则表达式匹配特定的URL

[英]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 我曾经在php中构建了一个程序,它使用非常特定的正则表达式来匹配链接,但是这种模式似乎不适用于java,我试图找到相当于java的java

"~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? 在PHP中,这将匹配http://t.co/UURRNlrKhttp://bit.ly/AenG5W这样的链接,这将是一个java的等价物吗?

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 输出= http://t.co/UURRNlrK

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

Output = http://bit.ly/AenG5W 输出= http://bit.ly/AenG5W

Here is a nice Regex Tutorial for java. 这是一个很好的Java正则教程

I tried this: 我试过这个:

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.

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