基础语法
\
转义字符,\,&,+等是特殊字符,需要在正则表达式和java中都进行一次转义
System.out.println("a&b".matches("a\\&b")); //true
System.out.println("a#b".matches("a\\#b")); //true
System.out.println("a%b".matches("a\\%b")); //true
System.out.println("a\b".matches("a\\\b")); //true
\d
表示匹配任意数字,但是有且只能匹配单个数字
System.out.println("1234".matches("12\\d4")); //true
System.out.println("1234".matches("123\\d")); //true
System.out.println("12345".matches("12\\d5")); //false
\D
大写表示取反,即取 \d 的反面,非数字,也只能表示单个数字
System.out.println("1234".matches("12\\D4")); //false
System.out.println("1234".matches("123\\D")); //false
System.out.println("123w".matches("123\\D")); //true
{n,m}
表示匹配n至m个字符或者数字,与\d,\D,\w等结合使用,匹配n次,可以表示为{n},匹配至少n次表示为{n,},最多m次表示为{0,m}
System.out.println("1234".matches("\\d{4}")); //true
System.out.println("1234".matches("12\\d{1,}")); //true
System.out.println("1234".matches("12\\d{2,}")); //true
System.out.println("1234".matches("12\\d{0,1}")); //false
System.out.println("1234".matches("12\\d{0,2}")); //true
\w
单词[word]的简称,表示匹配一个字符,包括字母、下划线、数字,等价于[A-Za-z0-9_]
System.out.println("ab12".matches("\\w{4}")); //true
System.out.println("a_b".matches("\\w{1,}")); //true
System.out.println("a&b".matches("\\w{0,3}")); //false
System.out.println("a&b".matches("\\w\\&\\w")); //true, 注:&为特俗字符
\W
\w的取反,表示非字符,即[A-Za-z0-9_]反面
System.out.println("+".matches("\\W")); //true
System.out.println("_".matches("\\W")); //false
System.out.println(" ".matches("\\W")); //true
System.out.println("a&b".matches("\\w\\W\\w")); //true
\s
[space]的缩写,表示空格、回车、Tab键,包括Tab键打出来的空格[\t]和回车键打出来的[\n]
System.out.println(" ".matches("\\s")); //true
System.out.println("\t".matches("\\s")); //true
System.out.println(("\n").matches("\\s")); //true
System.out.println("\t \n".matches("\\s{3}")); //true
\S
\s的取反
System.out.println("a".matches("\\S")); //true
System.out.println("a".matches("\\s")); //false
.
匹配任意单个字符
System.out.println("&".matches(".")); //true
System.out.println("+".matches(".")); //true
System.out.println("ab".matches(".")); //false
System.out.println("ab".matches("a.")); //true
System.out.println("abc".matches("a.c")); //true
*
匹配任意字符,且不限制次数,包括0次,等价于{0,}
System.out.println("".matches("\\w*")); //true
System.out.println("rununun".matches("\\w*")); //true
System.out.println("ru*".matches("\\w{2}\\*")); //true,注:匹配*需要加转义字符
+
匹配至少一次,等价于{1,}
System.out.println("".matches("\\w+")); //false
System.out.println("r".matches("\\w+")); //true
System.out.println("ru".matches("\\w+")); //true
?
要么匹配 0 次,要么匹配 1 次,等价于{0,1}
System.out.println("".matches("\\w?")); //true
System.out.println("r".matches("\\w?")); //true
System.out.println("rn".matches("\\w?")); //false
^
对范围区间取反[^1-3]不在1-3范围内,
System.out.println("4".matches("[^1-3]")); //true
System.out.println("45".matches("[^1-3]")); //false 注:数字最大1-9
System.out.println("45".matches("[^1-3][^1-3]")); //true
System.out.println("r".matches("[1-3a-z]")); //true
System.out.println("r".matches("[^1-3a-z]")); //false
简单实例
System.out.println("Name:Aurora Age:18".matches("Name:\\w+\\s*Age:\\d{1,3}")); // true
System.out.println("其中还夹杂着一些无关紧要的数据".matches("Name:\\w+\\s*Age:\\d{1,3}")); // false
System.out.println("Name:Bob Age:20".matches("Name:\\w+\\s*Age:\\d{1,3}")); // true
System.out.println("错误的数据有着各种各样错误的格式".matches("Name:\\w+\\s*Age:\\d{1,3}")); // false
System.out.println("Name:Cassin Age:22".matches("Name:\\w+\\s*Age:\\d{1,3}")); // true
attern pattern1= Pattern.compile("Name:(\\w+)\\s*Age:(\\d{1,3})");
System.out.println(pattern1.matcher("Name:Aurora Age:18").matches()); //true
System.out.println(pattern1.matcher("Name:Bob Age:20").matches()); //true
System.out.println(pattern1.matcher("Name:Cassin Age:22").matches()); //true
match.group的使用
()将要取的值括起来,传递给Pattern,如果匹配成功【matcher.matches()为true】,从group为1的下标开始取,下标为0存储的是原字符串
Pattern pattern=Pattern.compile("Name:(\\w+)\\s*Age:(\\d{1,3})");
Matcher matcher=pattern.matcher("Name:Aurora Age:18");
if(matcher.matches()) {
String group1=matcher.group(1);
String group2=matcher.group(2);
System.out.println(group1);// 输出为 Aurora
System.out.println(group2);// 输出为 18
}
//正则表达式会默认向后尽可能的多匹配[贪婪模式],可以加?表示非贪婪模式
Pattern pattern1=Pattern.compile("(\\w+)(e*)");
Matcher matcher1=pattern1.matcher("LeetCode");
Pattern pattern2=Pattern.compile("(\\w+?)(e*)");
Matcher matcher2=pattern2.matcher("LeetCode");
if(matcher1.matches()) {
String group1=matcher1.group(1);
String group2=matcher1.group(2);
System.out.println("group1 = "+group1+", length = "+group1.length());
System.out.println("group2 = "+group2+", length = "+group2.length());
}
System.out.println("--非贪婪模式--");
if(matcher2.matches()) {
String group1=matcher2.group(1);
String group2=matcher2.group(2);
System.out.println("group1 = "+group1+", length = "+group1.length());
System.out.println("group2 = "+group2+", length = "+group2.length());
}
//group1 = LeetCode, length = 8
//group2 = , length = 0
//group1 = LeetCod, length = 7
//group2 = e, length = 1
String.split应用
System.out.println(Arrays.toString("二分,回溯,递归,分治".split("[,;\\s]")));
// [二分, 回溯, 递归, 分治]
System.out.println(Arrays.toString("搜索;查找;旋转;遍历".split("[,;\\s]")));
// [搜索, 查找, 旋转, 遍历]
System.out.println(Arrays.toString("数论 图论 逻辑 概率".split("[,;\\s]")));
// [数论, 图论, 逻辑, 概率]
String.replaceAll应用
System.out.println("二分,回溯,递归,分治".replaceAll("[,;\\s]+", ";"));
//二分;回溯;递归;分治
System.out.println("搜索;查找;旋转;遍历".replaceAll("[,;\\s]+", ";"));
//搜索;查找;旋转;遍历
System.out.println("数论 图论 逻辑 概率".replaceAll("[,;\\s]+", ";"));
//数论;图论;逻辑;概率
//replaceAll第二个参数还可以通过 $0 $1 来反向引用
System.out.println("二分,回溯,递归,分治".replaceAll("[,;\\s]+", "---$0---"));
//二分---,---回溯---,---递归---,---分治
System.out.println("搜索;查找;旋转;遍历".replaceAll("[,;\\s]+", "---$0---"));
//搜索---;---查找---;---旋转---;---遍历
System.out.println("数论 图论 逻辑 概率".replaceAll("[,;\\s]+", "---$0---"));
//数论--- ---图论--- ---逻辑--- ---概率