首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

匹配字符串的算法

2012-10-23 
求一个匹配字符串的算法大侠们我有这样一个需求:标示符 identifier:(/news/|/finace/)&fromucweb链接URL

求一个匹配字符串的算法
大侠们我有这样一个需求:

标示符 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([&].*)?";
[解决办法]
这个需用正则表达式。但是描述的不是很到位啊。
[解决办法]
探讨
是匹配(/news/|/finace/)&from=ucweb,即/news或/finace后面紧跟/&from=cuweb,还是说以/news/或/finace/开头,中间可以出现任意abroad/zhuanti,然后跟着&from=cuweb,随后任意,即&foo=sthelse可有可无

have a try
Java code
String rege……

热点排行