正则表达式
public static void main(String[] args) {
System.out.println("---------------------------常用字符-------------------------------------");
// \\d 表示一位数字
System.out.println("2312".matches("\\d\\d\\d\\d"));
// \\D 大写字母表示相反的,非
System.out.println("1dgf22".matches("\\d\\D\\D\\D\\d\\d"));
// \\s 表示空白字符 包含[ \t\n\x0B\f\r]
System.out.println("1 3".matches("\\d\\s\\s\\d"));
// \\w表示[a-zA-Z_0-9]
System.out.println("aAbedD".matches("\\w\\w\\w\\w\\w\\w"));
// [abc]表示是否是abcd字符中的某一个 [a-z]表示是否是a-z之间的字符
System.out.println("a".matches("[abc]"));
// && 是并且
System.out.println("h".matches("[a-z&&[a-z]]"));
System.out.println("h".matches("[a-d |[A-D]]"));
System.out.println("---------------------------常用基于数量的匹配-------------------------------------");
// ? 。 * + 基于数量的匹配
// .表示任意字符
System.out.println("a".matches("."));
// . 表示的一位字符
System.out.println("aa".matches(".a"));
// * 表示任意多个字符(0个或多个)
System.out.println("aaaa".matches("a*"));
// 注意:false,*表示的多个前面字符
System.out.println("abcd".matches("a*"));
// .是任意字符
System.out.println("abcd".matches("a.*"));
// *表示任意多个字符
System.out.println("3".matches("\\d*"));
// *表示0个 或多个
System.out.println("".matches("a*"));
// +表示一个或多个
System.out.println("a".matches("a+"));
// false,+表示一个或多个
System.out.println("".matches("a+"));
// ?表示0个或者1个
System.out.println("a".matches("a?"));
// false ?表示前方字符0个或者1个
System.out.println("aa".matches("a?"));
System.out.println("".matches("a?"));
System.out.println("----------------------------------------------------------------");
// {n} 精确次数,{n,} 至少n次,{n,m} 至少n次,最多m次
System.out.println("2023-01-01".matches("\\d{4}-\\d{1,2}-\\d{1,2}"));
System.out.println("-----------------------------应用范例-----------------------------------");
// .是任意字符,如果表示点,需要\\. 点出现0次或多次
System.out.println("234563.42".matches("\\d+\\.?\\d+"));
// 匹配一个电话号码
System.out.println("010-2132132-343".matches("\\d{3,4}-\\d{7}-\\d{2,5}"));
// 35以内的数字 \\d{1}|[12]{1}\\d{1}|[3]{1}[0-5]{1} 字符+数量
System.out.println("45".matches("\\d{1}|[1-2]\\d|3[0-5]"));
// 匹配一个ip地址 111.222.322.222
System.out.println("192.168.111.1".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));
// 匹配一个身份证号
System.out.println("13068122220803532X".matches("\\d{15}||\\d{18}||\\d{17}[X]"));
// 匹配一个电子邮件 [\\w\\.]* @后除了出现\\w之外,还可以出现\\. 0次或多次 .cn前必须是\\w
System.out.println("aa-a@gmail-p.com.cn".matches("[\\w-]+@[\\w\\.-]*\\w+\\.\\w{2,6}"));
System.out.println("---------------------边界匹配---------------------------");
// ^在[]中是非的意思,[^abc] 除了abc外的所有字符,^不在[]中表示以xx开始
System.out.println("helloworld".matches("^h.*"));
System.out.println("helloworld".matches("^h\\w+"));
// $表示结尾
System.out.println("1you1".matches("\\w*\\d$"));
}
Pattern和Matcher
注意:贪婪模式和非贪婪模式
public static void main(String[] args) {
String str = "123sadfsada2324132ss";
System.out.println(str.replaceAll("\\d","*"));
System.out.println(str.replaceAll("\\d+","*"));
// 省略最后四位为*
System.out.println("d1234565678".replaceAll("\\d{4}$","****"));
System.out.println("----------------");
str = "130681198908035323,130681200108035323,130681200008035323";
// 使用括号进行分组 由 \d{6}\d{8}\d{4} 改为 (\d{6})(\d{8})\d{4}
// Pattern pattern = Pattern.compile("(\\d{6})(\\d{8})\\d{4}");
Pattern pattern = Pattern.compile("((\\d{6})(\\d{8}))\\d{4}");
Matcher matcher = pattern.matcher(str);
while (matcher.find()){
System.out.println(matcher.group());
System.out.println("生源地:"+matcher.group(1)+",出生日期:"+matcher.group(2)+":"+matcher.group(3));
}
// 贪婪模式,.*会匹配所有信息,此处输出就是 字段</td><td>类型</td><td>描述
String html = "<html><td>字段</td><td>类型</td><td>描述</td></html>";
Pattern p = Pattern.compile("<td>(.*)</td>");
Matcher m = p.matcher(html);
while (m.find()) {
System.out.println(m.group(1));
}
// 非贪婪模式,仅仅匹配第一个符合的结尾,特别注意:?在 * + 之后表示使用非贪婪模式
html = "<html><td>字段</td><td>类型</td><td>描述</td></html>";
p = Pattern.compile("<td>(.*?)</td>");
m = p.matcher(html);
while (m.find()) {
System.out.println(m.group(1)+"["+m.start()+","+m.end()+"]");
}
}