[abc]?:匹配0个或者1个,但匹配“d”的时候,结果是false,而不是说匹配了0个返回true。匹配0个,相当于匹配了""空字符串。
public static void demo1() {
/*
* [abc]? : 匹配0个("")或者1个(a | b | c)
*/
String regex = "[abc]?";
//true
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
//false
System.out.println("bc".matches(regex));
System.out.println("d".matches(regex));
//true
System.out.println("".matches(regex));
}
本文深入探讨了正则表达式中?[abc]的使用细节,解释了其如何匹配0个或1个字符(a、b或c),并提供了具体的匹配案例,包括真值与假值情况的对比。
119

被折叠的 条评论
为什么被折叠?



