java-正则表达式2
package com.day10.Regex正则表达式;
public class Demo2Regex {
/**
* A:字符类
* [abc] a、b 或 c(简单类)
* [^abc] 任何字符,除了 a、b 或 c(否定)
* [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
* [a-d[m-p]] a到d或m到p:[a-dm-p](并集)
* [a-z&&[def]]d、e或f(交集),相当于是def
* [a-z&&[^bc]]a到z,除了b和c:[ad-z](减去)
* [a-z&&[^m-p]]a到z,而非m到p:[a-lq-z](减去)
* [0-9] 0到9的字符都包括
*/
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("c".matches(regex));//true
System.out.println("d".matches(regex));//false
System.out.println("%".matches(regex));//false
String regex1="[^abc]";
System.out.println("a".matches(regex1));//fasle
System.out.println("b".matches(regex1));//false
System.out.println("c".matches(regex1));//false
System.out.println("d".matches(regex1));//true
System.out.println("%".matches(regex1));//true
System.out.println("10".matches(regex1));//false,中括号代表单个字符,10是两个字符
String regex2="[a-zA-Z]";
System.out.println("a".matches(regex2));//true
System.out.println("A".matches(regex2));//true
System.out.println("z".matches(regex2));//true
System.out.println("Z".matches(regex2));//true
System.out.println("%".matches(regex2));//false
}
}