我正在寻找一种方法来将一个通配符添加到一个字符串中,这样我就可以从近似匹配中搜索,所以如果我在字符串列表中搜索单词" b * ke" (其中*是通配符),我会得到{bake,bike,byke,boke},这在java中可能吗?
答案 0 :(得分:1)
您已经找到了所需的“正则表达式”。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "b[a-z]{1}ke";
final String string = "bake, bike, byke, boke";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
答案 1 :(得分:0)
一切都是真的
System.out.println("bike".matches("b.ke"));
System.out.println("bake".matches("b.ke"));
System.out.println("b1ke".matches("b.ke"));
System.out.println("b123ke".matches("b.+ke"));
System.out.println("b[wait_for_it]ke".matches("b.+ke"));