用任何字母替换字符串中的字符
Replacing a char in string with any letter
我正在寻找一种将通配符字符添加到字符串的方法,这样我就可以从近似匹配项中进行搜索,所以如果我在我的字符串列表中搜索单词 "b*ke"(其中 * 是通配符),我会得到 {bake, bike, byke, boke} 回来,这可能在 java 吗?
您已找到您需要的"regular expressions"。
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));
}
}
一切都是真的
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"));
我正在寻找一种将通配符字符添加到字符串的方法,这样我就可以从近似匹配项中进行搜索,所以如果我在我的字符串列表中搜索单词 "b*ke"(其中 * 是通配符),我会得到 {bake, bike, byke, boke} 回来,这可能在 java 吗?
您已找到您需要的"regular expressions"。
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));
}
}
一切都是真的
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"));