java-正则表达式4
package com.day10.Regex正则表达式;
public class Demo4Regex {
/**
*常见对象(数量词)
* A:Greedy 数量词
* X? X,出现一次或一次也没有
* X* X,零次或多次
* X+ X,一次或多次
* X{n} X,恰好 n 次
* X{n,} X,至少 n 次
* X{n,m} X,至少 n 次,但是不超过 m 次 [n,m]
*/
public static void main(String[] args) {
String regex="[abc]?";
System.out.println("a".matches(regex));//true
System.out.println("b".matches(regex));//true
System.out.println("d".matches(regex));//false
System.out.println("".matches(regex));//true
System.out.println(" ".matches(regex));//false
String regex1="[abc]*";
System.out.println("".matches(regex1));//true
System.out.println("abc".matches(regex1));//true
System.out.println("a".matches(regex1));//true
String regex2="[abc]{5}";
System.out.println("abcba".matches(regex2));//true
System.out.println("abcb".matches(regex2));//false
System.out.println("abcbaabccb".matches(regex2));//false
String regex3="[abc]{3,5}";
System.out.println("abc".matches(regex3));//true
System.out.println("ab".matches(regex3));//false
System.out.println("abcbc".matches(regex3));//true
System.out.println("abcbac".matches(regex3));//false
}
}