嗨,我正在做一个价格检查器,它会从超市网站返回最便宜的商品名称和价格。代码中的大多数价格都采用 ...price:3.14... 格式,但有些价格只是 ...price:2...
这是已经输入示例搜索的网站,如果您愿意,请查看源代码 http://www.tesco.ie/groceries/product/search/default.aspx?searchBox=ham
我的正则表达式通常会从源代码中提取 18/20 个项目,但是当它的价格仅为 1 位数时,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;
}