我有一个方法,其输入是一个 URL 字符串。我想要不同类型的 URL(我在下面提到的 5 个示例 URL)
String url1 = "http://domainName/contentid/controller_hscc.jsp?q=1" ;
String url2 = "http://domainName/contentid/controller.jsp?waitage=1" ;
String url3 = "http://domainName/contentid/controller_runner.jsp" ;
String url4 = "http://domainName/contentid/jump.jsp?q=5" ;
String url5 = "http://domainName/contentid/jump.jsp" ;
我需要查找此 URL 中是否包含controller*.jsp 模式。如果是这样,我将不得不为其编写一些其他逻辑。
现在,我需要知道如何在java中编写*controller*.jsp
模式
我用这种方式写了一个正则表达式,它总是返回 false
boolean retVal = Pattern.matches("^(controller)*", url) ;
PS:我使用JDK1.4
EDIT-1
我尝试了以下方法。还是没干
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld{
public static void main(String []args){
String url = "http://domainName/contentid/controller_hscc.jsp?q=1" ;
//String url = "baaaaab" ;
String regex = "/controller(\\w+)?\\.jsp*" ;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(url);
System.out.println(m.matches());
}
}
最佳答案
关于java - URL 的正则表达式模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23288039/