通过正则表达式判断淘宝宝贝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;
	}

你可能感兴趣的:(正则表达式)