java 过滤出以 A1~A11 开头的字符串
时间: 2023-06-01 20:05:19 浏览: 147
可以使用正则表达式^A1[1-1]?|^A[2-9]$来匹配以A1到A11开头的字符串。例如,在Java中可以这样写:
String regex = "^A1[1-1]?|^A[2-9]$";
String str = "A10 is not a match. A1 is a match. A2 is a match.";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
输出:
A1
A2
阅读全文