【发布时间】:2014-02-10 09:26:58
【问题描述】:
已经有一段时间了,所以我认为是时候寻求一些急需的帮助了。我在动作侦听器中有以下 if 语句('但是' - 是被单击以发生以下情况的搜索按钮)。我的类中有一个 arrayList(用于保存数据库中的数据),调用特定记录有效完美。我正在尝试创建一个搜索文本字段(下面名为“tf”)。我只能通过键入与正在搜索的内容完全匹配来进行搜索,但我希望能够对 tf.String 中的任何字符串使用部分匹配。注释掉的所有内容都是我一直在测试的不同内容,因此可以忽略。
声明: if( srch.getText().equals("您已选择按电影搜索:" ) ...这用于确保该人仅在面板包含此标题时才进行搜索,因为在同一面板上也有流派搜索。只有标题改变。
if(source.equals(but)){
//String b = a.get(0).get(1);
String result = tf.getText();
Pattern pat = Pattern.compile("[a-z]+");
Matcher m = pat.matcher(result);
//boolean bool = m.matches();
int i = 0;
//al.contains(result) && pat.matcher(a.get(i).get()).matches())
// && z.contains(result)
if( srch.getText().equals("You have chosen to search by movie: " )){
for (ArrayList<String> al : a){
if (pat.m(a.get(i).get(0)).matches()){
System.out.println(a.get(i).get(0)); //movie name
System.out.println(a.get(i).get(1)); //movie desc
}
i++;
}
//ta.setText(b);
}
else{
ta.setText("Please try searching by movie");
}
}
总之,一切正常。我只需要一个正则表达式代码来查找部分匹配项以及一种将其添加到我的循环中的方法。我添加了尽可能少的代码,以免浪费任何人的时间,所以如果需要任何其他代码,请告诉我。非常感谢。最终,任何完整或部分匹配都会显示电影名称和描述。
【问题讨论】:
标签: java regex arraylist jtextfield