[英]Java Regex, Multiple expressions with variables
So, I am trying to make a function that will visit one page, grab the ID and TYPE of an item, then visits a link and plugs in the ID and TYPE to get the ITEM NAME. 因此,我试图制作一个将访问一页,获取项目ID和TYPE,然后访问链接并插入ID和TYPE以获得项目名称的函数。 Then it will add all of this into an ArrayList like. 然后它将所有这些添加到ArrayList之类的。 new List(id,type,name); 新的List(id,type,name);
public static ArrayList<Kad> getDetails(final String strHTML, final String strHTML2) {
final ArrayList<Kad> kads = new ArrayList<Kad>();
try {
final Pattern regex = Pattern
.compile(
"id=(\\d+)\\&tab=([a-zA-Z]+)",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
final Pattern regex2 = Pattern
.compile(
"font-weight:bold; font-size:11px;\">([\\w\\s]+)</div>",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
final Matcher regexMatcher = regex.matcher(strHTML);
final Matcher regexMatcher2 = regex2.matcher(strHTML2);
String ids = "";
while (regexMatcher.find() && regexMatcher2.find()) {
final int id = Integer.parseInt(regexMatcher.group(1));
final String tab = regexMatcher.group(2);
final String name = regexMatcher2.group(1);
if (!ids.contains(id + "|") && !ignoreList.contains(id)) {
kads.add(new Kad(id,tab,name));
ids += id + "|";
}
}
return kads;
} catch (final Exception ex) {
ex.printStackTrace();
}
return new ArrayList<Kad>();
}
This code works to get the id, type and name of the item. 此代码用于获取商品的ID,类型和名称。 But, I can't get a list of IDs since this function gets the ID and then the name. 但是,我无法获取ID列表,因为此函数先获取ID,然后获取名称。 What is the best way to break this up into two functions where one gets the ID and TYPE, then the other gets the name. 将其分解为两个函数的最佳方法是:一个函数获取ID和TYPE,然后另一个函数获取名称。
This method is called using: 使用以下方法调用此方法:
data = wrapper.post("LINK1", "tab=" + "food" + "&page=" + "1", "");
data2 = wrapper.post("LINK2", "tab=" + "CURRENT ITEM TYPE IN LIST GOES HERE" + "&id=" + "CURRENT ITEM ID IN LIST GOES HERE", "");
final ArrayList<Kad> kadList = Kad.getDetails(data, data2);
I tried to break it up into two seperate methods, but could not figure out how to add the name found in method 2 to the already created list of ID and TYPE in method 1. 我试图将其分解为两个单独的方法,但无法弄清楚如何将方法2中找到的名称添加到方法1中已创建的ID和TYPE列表中。
EDIT: Okay, so after the suggested solution below, I can now fetch id and name in two seperate methods. 编辑:好的,所以在下面建议的解决方案之后,我现在可以在两个单独的方法中获取id和name。 The problem now is that method 2( the get name method) Is giving every single ID the same name(the first name that is searched). 现在的问题是方法2(get名称方法)为每个ID赋予相同的名称(被搜索的名字)。 How Do i solve this? 我该如何解决?
public static ArrayList<Kad> findItemName2(final int id, final String tab, final String strHTML) {
final ArrayList<Kad> names = new ArrayList<Kad>();
try {
final Pattern regex = Pattern
.compile(
"font-weight:bold; font-size:11px;\">([\\w\\s]+)</div>",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
final Matcher regexMatcher = regex.matcher(strHTML);
String ids = "";
while (regexMatcher.find()) {
final String name2 = regexMatcher.group(1);
if (!ids.contains(name2 + "|")) {
names.add(new Kad(id,tab, name2));
name = name2;
ids += name2 + "|";
}
}
return names;
} catch (final Exception ex) {
ex.printStackTrace();
}
return new ArrayList<Kad>();
}
You can try as below 您可以尝试如下
method1
and method2
- one will for grabbing id
and type
and another will for name
. 创建2个单独的方法method1
和method2
一个将用于获取id
和type
,另一个将用于name
。 getDetails
method and move it into these methods. 隔离现有getDetails
方法中存在的代码,并将其移入这些方法。 id
, type
and name
inside Kad
class. 在Kad
类中为id
, type
和name
创建getter和setter。 Kad
class instance and call method1
and method2
on it and call the respective setters inside these method1
& method2
to set the value for these fields. 创建一个Kad
类实例,并在其上调用method1
和method2
,并调用这些method1
和method2
内部的相应设置器以设置这些字段的值。 Once, method1
and method2
are executed - you have id
, type
and name
values set into three different fields which can be accessed using the getters. 一旦执行了method1
和method2
您将id
, type
和name
值设置为三个不同的字段,可以使用getter进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.