java - 正则表达式

标签 java regex

我有一些代码:

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/

相关文章:

java - 如何在java/scala中锁定免费使用两个ConcurrentHashMap?

c++ - 将 tr1::regex 与 unicode 字符串一起使用时出现问题

java - 如何在java中使用正则表达式解析给定的字符串

java - 使用 BouncyCaSTLe API 时出现 RSA key 生成错误

java - 如何修复/学习有助于理解基本方法和类、Java代码和单词问题

java - 如何从 X509 证书中提取 AuthorityKeyIdentifier

java - 复制引用数组

ruby - ruby 正则表达式中 ^ 、 $ 和\A 、\Z 之间的区别

Python/Django - 如何创建仅允许字母和空格的正则表达式(RegexValidator)?

jquery - 使正则表达式中的括号可选