- 判断 第一位 第一是Z 或是 W
String str ="Z";
String regex3 = "[Z|W]{1}";//第一是Z 或是 W
System.out.println(Pattern.matches(regex3, str));
- 判断 第一位 第一是Z 或 W ,第2-3位是否为数字
String str ="Z01";
String regex3 = "[Z|W]{1}\\d{2}";
System.out.println(Pattern.matches(regex3, str));
总结:
{2} 表示长度两位
\\d 表示数字
[W|Z] 表示Z或是W
{1}{2} 判断的字符串长度为3,如果长度为4即使表达式正确的也返回“false”;