匹配字符串的算法
阅读
大侠们我有这样一个需求:
标示符 identifier:(/news/|/finace/)&from=ucweb
链接 URL : /news/abroad/zhuanti?nid=122334&from=ucweb&foo=sthelse
求解URL是否匹配identifier?
我的思路是以 (, ) , | , &四个字符分解标示符,然后计算分别计算/news/, /finance/, from=ucweb的布尔值,然后根据运算顺序求最终布尔值。
但是不知道该如何实现,或许可以借鉴数学运算的原理,哪位高人可以指点一下?
非常感谢!
------解决方案--------------------
正则表达式就可以搞定了吧
------解决方案--------------------
^[a-zA-Z0-9/]*((/news/)|(/finace/))[a-zA-Z0-9/]*(from=ucweb)[a-zA-Z0-9/]*$
随便写了一个,没调试过
------解决方案--------------------
- Java code
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));
------解决方案--------------------
是匹配(/news/|/finace/)&from=ucweb,即/news或/finace后面紧跟/&from=cuweb,还是说以/news/或/finace/开头,中间可以出现任意abroad/zhuanti,然后跟着&from=cuweb,随后任意,即&foo=sthelse可有可无
have a try
- Java code
String regex = "/(news|finance)/.*?[&]from=ucweb([&].*)?";
------解决方案--------------------
这个需用正则表达式。但是描述的不是很到位啊。
------解决方案--------------------
阅读
上一篇:j2se-j地图看性能 下一篇:返回列表