通过正则表达式判断淘宝宝贝URL是否合法,返回获取宝贝ID

	/**
* 根据宝贝URL截取宝贝ID
* http://auction1.taobao.com/auction/item_detail-0db1-d57b90f4c406fe1ee1517884dafe338b.jhtml
* 截取后为32位的字符串:d57b90f4c406fe1ee1517884dafe338b
* http://item.taobao.com/auction/item_detail.htm?itemID=1b70b4c3fb32cf0e24af9a649ad5360d
* 截取后为32位的字符串:1b70b4c3fb32cf0e24af9a649ad5360d
* 如下几种宝贝URL均合法:
* http://item.taobao.com/auction/item_detail-0db2-a159acabbeedeb61ea92231371adae67.jhtml
* http://auction1.taobao.com/auction/item_detail-0db1-6ce724f828e554364f6bb8cd4fdf0249.jhtml
* http://item.taobao.com/auction/item_detail.htm?itemID=1b70b4c3fb32cf0e24af9a649ad5360d&ali_refid=a3_419095_1006:380074963:6:%B7%DB:504f81b2bd89eb72144729a403c22c10
* http://item.taobao.com/auction/item_detail.jhtml?item_id=f19580fd3d5ec2395ff6e7d4192b9230&x_id=0db1
* http://item.taobao.com/auction/item_detail--2c8338b253d3beaa41afb51f610e2eb5.jhtml
* @param auctionUrl
* @return
*
*/

private String parseUrl(String auctionUrl){
String ret = "";
try{
String regex1 = "http://(item|auction1)\\.taobao\\.com/auction/item_detail-(\\w{4}|\\w{3})-(\\w{32})\\.jhtml.*";
String regex2 = "http://(item|auction1)\\.taobao\\.com/auction/item_detail\\.(htm|jhtml)\\?(itemID|item_id)=(\\w{32}).*";
String regex3 = "http://item.taobao.com/auction/item_detail--(\\w{32}).jhtml";
Pattern pattern1 = Pattern.compile(regex1,Pattern.CASE_INSENSITIVE);
Matcher matcher1 = pattern1.matcher(auctionUrl);

Pattern pattern2 = Pattern.compile(regex2,Pattern.CASE_INSENSITIVE);
Matcher matcher2 = pattern2.matcher(auctionUrl);

Pattern pattern3 = Pattern.compile(regex3,Pattern.CASE_INSENSITIVE);
Matcher matcher3 = pattern3.matcher(auctionUrl);

if(matcher1.matches()){
ret = matcher1.group(3);
return ret;
}else if(matcher2.matches()){
ret = matcher2.group(4);
return ret;
}else if(matcher3.matches()){
ret = matcher3.group(1);
return ret;
}
}catch(Exception e){
e.printStackTrace();
}
return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值