大侠们我有这样一个需求:标示符 identifier:(/news/|/finace/)&from=ucweb
链接   URL :   /news/abroad/zhuanti?nid=122334&from=ucweb&foo=sthelse求解URL是否匹配identifier?我的思路是以 (, ) , | , &四个字符分解标示符,然后计算分别计算/news/, /finance/, from=ucweb的布尔值,然后根据运算顺序求最终布尔值。但是不知道该如何实现,或许可以借鉴数学运算的原理,哪位高人可以指点一下?非常感谢!

解决方案 »

  1.   

    ^[a-zA-Z0-9/]*((/news/)|(/finace/))[a-zA-Z0-9/]*(from=ucweb)[a-zA-Z0-9/]*$随便写了一个,没调试过
      

  2.   

    "(/news/|/finace/)&from=ucweb"不是正则表达式,正则里面没有 &(and)这样的用法。
      

  3.   

        String regex = "/(news|finance)/.*[?&]from=ucweb([&#].*|$)";
        System.out.println("/news/abroad/zhuanti?nid=122334&from=ucweb&foo=sthelse".matches(regex));
        System.out.println("/news/?from=ucweb&foo=sthelse".matches(regex));
        System.out.println("/news/abroad/zhuanti?nid=122334&from=ucweb".matches(regex));
        System.out.println("/news/abroad/zhuanti?nid=122334&from=ucweb#anchor".matches(regex));
        System.out.println("/news/abroad/zhuanti?nid=122334&xfrom=ucwebtt".matches(regex));
        System.out.println("/news/abroad/zhuanti?nid=122334&xfrom=ucweb".matches(regex));
        System.out.println("/news/abroad/zhuanti?nid=122334&from=ucwebtt".matches(regex));
        System.out.println("/news/abroad/zhuanti?xfrom=ucweb".matches(regex));
        System.out.println("/news/abroad/zhuanti?from=ucwebtt".matches(regex));
      

  4.   

    "&"在这里是"and"的意思,不是url里面那个&。
      

  5.   

    是匹配(/news/|/finace/)&from=ucweb,即/news或/finace后面紧跟/&from=cuweb,还是说以/news/或/finace/开头,中间可以出现任意abroad/zhuanti,然后跟着&from=cuweb,随后任意,即&foo=sthelse可有可无have a try
    String regex = "/(news|finance)/.*?[&]from=ucweb([&].*)?";
      

  6.   

    这个regex有问题,注意我的测试用例
      

  7.   

    这里|就表示或,&标识并,是计算查找字符串的真伪,既不是正则,也不是url里面的连接字符