我有一些代码:
String url1 = "http://shop.lenovo.com/SEUILibrary/controller/e/web/LenovoPortal/en_US/cart.workflow:ShowCart?shopping-menu-s...";
String url2 = "shop.lenovo.com/(.*)/en_US/cart";
Pattern p = Pattern.compile(url2);
Matcher m = p.matcher(url1);
但是 m.matches() 对我来说返回 false。为什么?
最佳答案
请尝试这个而不是您的代码
String url1 = "http://shop.lenovo.com/SEUILibrary/controller/e/web/LenovoPortal/en_US/cart.workflow:ShowCart?shopping-menu-s...";
String subS1 = "shop.lenovo.com/";
Pattern p1 = Pattern.compile(subS1);
Matcher match1 = p1.matcher(url1);
String subS2 = "en_US/cart";
Pattern p2 = Pattern.compile(subS2);
Matcher match2 = p2.matcher(url1);
if (match1.find() && match2.find())
{
// Whatever you like
}
关于java - 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43091930/