想利用java里pattern功能处理对多个关键字的匹配查询
发布网友
发布时间:2022-04-27 10:04
我来回答
共2个回答
热心网友
时间:2022-04-22 22:20
Pattern pattern = Pattern.compile("切");
Matcher matcher = pattern.matcher(txt);
Matcher matcher2 = Pattern.compile("蒸").matcher(txt);
Matcher matcher3 = Pattern.compile("煮").matcher(txt);
Matcher matcher4 = Pattern.compile("炖").matcher(txt);
Matcher matcher5 = Pattern.compile("炸").matcher(txt);
boolean a = false;
boolean b = false;
boolean c = false;
boolean d = false;
boolean e = false;
while ((a = matcher.find())|| (b = matcher2.find())||(c = matcher3.find())||( d = matcher4.find())||(e = matcher5.find())) {
String msg ="";
if(a){
msg = ":" + matcher.group() + ":";
}else if(b){
msg = ":" + matcher2.group() + ":";
}else if(c){
msg = ":" + matcher3.group() + ":";
}else if(d){
msg = ":" + matcher4.group() + ":";
}else if(e){
msg = ":" + matcher5.group() + ":";
}
System.out.println(msg);
}
只不过这样比较麻烦,你可以自己精炼下,我想知道,你提取关键字是不是只是想确定是否包含,如果是你可以不用这个方法
热心网友
时间:2022-04-22 23:38
Pattern pattern = Pattern.compile("[切蒸煮炖炸]");