正则表达式将找不到所有必需的项目

时间:2013-04-17 16:48:07

标签: java regex

您好我正在制作价格检查器,从超市网站返回最便宜的商品名称和价格。 代码中的大多数价格都是格式......价格:3.14 ......但有些价格只是......价格:2 ......

这是已输入示例搜索的网站,如果您愿意,请查看源代码 http://www.tesco.ie/groceries/product/search/default.aspx?searchBox=ham

我的正则表达式,我通常会从源代码中获取18/20项,但当它只达到1位数的价格时,arraylist就会变得不同步。

我的问题是如何让正则表达式获取两种类型并将它们添加到arraylist以使其保持同步。

这是得到价格的正则表达式

String priceFinder =“price:(\ d {1,3})(。\ d {1,2})”;

如果有帮助,这里有更多代码

  public static Product addProducts(String item)  throws Exception {

            //@SuppressWarnings("resource")

            productList.clear();
            item = checkCommonItems(item);
            item = item.replaceAll("\\s+", "%20");

            URL oracle = new URL("http://www.tesco.ie/groceries/product/search/default.aspx?searchBox="+item);
            BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

            String inputLine;
            String name, price;
            int productArrayNumber = 0;

            String nameFinder = "name:\"([\\w{1,15} ]*)";
            String priceFinder = "price:(\\d{1,3})(.\\d{1,2})";
            while ((inputLine = in.readLine()) != null){
                    Pattern namePattern = Pattern.compile(nameFinder);              
                    Matcher nameMatcher = namePattern.matcher(inputLine);
                    Pattern pricePattern = Pattern.compile(priceFinder);
                    Matcher priceMatcher = pricePattern.matcher(inputLine);

                    while(nameMatcher.find()){
                            exists = false;
                            name = nameMatcher.group(1);
                            for(int i = 0; i < productList.size();i++)
                            {
                                    Product productExists = productList.get(i);
                                    if(productExists.getProductName().equals(name))
                                    {
                                            exists = true;
                                    }                              
                            }
                            if(exists== false)
                            {

                                    Product productNew =new Product(name,null);
                                    productList.add(productNew);
                            }

                    }
                    while(priceMatcher.find() && productArrayNumber<productList.size()){
                            price = priceMatcher.group(1);
                            price = price + priceMatcher.group(2);
                            Product productEdit = (Product) productList.get(productArrayNumber);
                            productEdit.setProductPrice(price);    
                            productList.set(productArrayNumber, productEdit);              
                            productArrayNumber++;
                    }
            }
            Product cheapest = null;
            if(productList.size() != 0)
            {

                    cheapest = productList.get(0);
                    for (int a = 0; a < productList.size()-1; a++)
                    {
                        System.out.println(productList.get(a));
                            Double chpPrice = Double.parseDouble(cheapest.getProductPrice());
                            Double cmpPrice = 500.0;
                            if(productList.get(a).getProductPrice() != null)
                            {
                                    cmpPrice = Double.parseDouble(productList.get(a).getProductPrice());
                                    if(chpPrice > cmpPrice)
                                    {
                                            cheapest = productList.get(a);
                                    }
                            }
                    }
                    in.close();
            }

            return cheapest;
    }

1 个答案:

答案 0 :(得分:0)

我认为您的问题仅在正则表达式中,因为您声称它对包含.\\d{1,2}部分的价格有效。你的问题可能是你没有逃避.,因为它是正则表达式中的特殊字符。另外,在将.\\d{1,2}添加到price之前,您不会检查是否price:(\\d{1,3}(\\.\\d{1,2})?)

尝试price = priceMatcher.group(1);并使用{{1}}