JavaSe的学习也已经接近尾声了,还有很多笔记没整理,先整理正则表达式的
后面整理的有些不是太认真,都以后在整理
1.什么是正则表达式
Regular Expression:它由一组具有特定含义的字符串组成,通常用于匹配、查找和替换文本。
java专门提供了java.util.regex包,用来处理正则
同时在字符串对象中,提供如下方法,可以使用正则
replaceAll()
replaceFirst()
split()
matches()
2.元字符
常用的元字符 | |
---|---|
代码 | 说明 |
. | 匹配除换行符(\n)以外的任意字符 |
\w | 匹配大小写字母或数字或下划线或汉字0-9、a-z、A-Z、_(下划线)、汉字和其他国家的语言符号 |
\W | 匹配非字母或数字或下划线或汉字,跟\w正好相反 |
\s | 匹配任意的空白符 |
\S | 匹配任意非空白符 |
\d | 匹配数字 |
\D | 匹配非数字 |
\b | 匹配单词的开始或结束 |
^ | 匹配字符串的开始 |
$ | 匹配字符串的结束 |
[ ] | 匹配[]中列举的字符 |
package com.study_re;
public class Test01 {
public static void main(String[] args) {
// . 表示任意字符
System.out.println("a".matches(".")); // true
System.out.println("ba".matches(".a")); // true
// 单独匹配一个点
System.out.println(".".matches("\\.")); // true
// . 可以匹配任意字符 除了 \n
System.out.println("\n".matches(".")); // false
// \\w 可以匹配 a-z A-Z _
System.out.println("a".matches("\\w")); // true
System.out.println("1".matches("\\w")); // true
System.out.println("_".matches("\\w")); // true
System.out.println("?".matches("\\w")); // false
// \\W 匹配和\\w相反的内容
// \\d 可以匹配数字(En:digital)
System.out.println("1".matches("\\d")); // true
System.out.println("a".matches("\\d")); // false
// \\D 匹配非数字
// \\s 匹配空格
System.out.println(" ".matches("\\s\\s")); // true
// 制表符也是空格
System.out.println("\t".matches("\\s")); // true
// \\S 匹配非数字
// [ab] 匹配a或b
System.out.println("b".matches("[abcde]")); // 匹配a-e
System.out.println("b".matches("[a-e]")); // 匹配a-e
// \\w 可以这样写
System.out.println("a".matches("[a-zA-Z0-9_]"));
// ^ 匹配开头,也可以是反义的意思
System.out.println("a".matches("^[a]")); // true
System.out.println("cbc".matches("^[abc]..")); // true
// 反义 ^[a] 匹配除了a以外的其它字符
System.out.println("1".matches("[^a]")); // true
}
}
3.反义符
常用的反义代码 | |
---|---|
代码/语法 | 说明 |
\W | 匹配任意不是字母,数字,下划线,汉字的字符 |
\S | 匹配任意不是空白符的字符 |
\D | 匹配任意非数字的字符 |
\B | 匹配不是单词开头或结束的位置 |
[ ^x ] | 匹配除了x以外的任意字符 |
[ ^aeiou ] | 匹配除了aeiou这几个字母以外的任意字符 |
4.重复(位数/长度)
常用的限定符 | |
---|---|
代码/语法 | 说明 |
* | 重复零次或更多次 |
+ | 重复一次或更多次 |
? | 重复零次或一次 |
{n} | 重复n次 |
{n,} | 重复n次或更多次 |
{n,m} | 重复n到m次 |
package com.study_re;
public class Test02 {
public static void main(String[] args) {
// * 匹配0或更多次
// + 匹配1或更多次
// ? 匹配0或1次
System.out.println("Liutao".matches("[A-Z][a-z]*")); // 匹配大写字母开头的单词
System.out.println("Liutao".matches("[A-Za-z]*")); // 匹配单词
System.out.println("Liutao18".matches("[A-Za-z]+[\\w]*")); // true
System.out.println("1".matches("[0-9]?[\\d]")); // true
System.out.println("111".matches("[0-9]?[\\d]")); // false
// {n} 重复n次
// {n,} 重复n次或更多次
// {n,m} 重复n次到m次
System.out.println("1sadfasdf11".matches("\\w{5}"));
System.out.println("1sadfasdf11".matches("\\w{5,}"));
System.out.println("1sadfasdf11".matches("\\w{5,9}"));
}
}
5.分组
这个分组就是如果没有捕获对象的话,获取值group()就不用传递参数,如果有一个或多个捕获对象,那么group()就需要传递索引用来获取具体的捕获对象
===Pattern===
static Pattern compile(String regex) 将给定的正则表达式编译为模式
Matcher matcher(CharSequence input) 创建一个匹配器,匹配给定的输入与此模式
===Matcher===
boolean find() 尝试找到匹配模式的输入序列的下一个子序列
String group() 返回与上一个匹配匹配的输入子序列
Matcher reset() 重设此匹配器
int start() 返回上一个匹配的起始索引
int end() 返回最后一个字符匹配后的偏移量
String group(int group) 返回在上一次匹配操作期间由给定组捕获的输入子序列
package com.study_re;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test03 {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("今年我16岁了,18岁就要成年了");
System.out.println(m.find()); // true
System.out.println(m.group()); // 16
m.reset(); // 重设匹配器
System.out.println(m.find()); // true
System.out.println(m.group()); // 16
while (m.find()) {
System.out.println(m.group() + "--" + m.start() + "--" + m.end());
}
// 分组
// Pattern p = Pattern.compile("(\\d+)"); // 注意这个地方 "(\\d+)" 表示的是捕获组
// Matcher m = p.matcher("今年我16岁了,18岁就要成年了");
// while(m.find()) {
// System.out.println(m.group(1));
// }
}
}
6.贪婪与懒惰
在Java中正则默认是贪婪模式(个别语言中也可能是非贪婪模式),贪婪模式就是总会尝试匹配更多的字符。
非贪婪模式则反之,总是尝试匹配尽可能少的字符。
在*、?、+、{m,n}后面加上?,可以将贪婪模式变成非贪婪模式(懒惰模式)。
懒惰限定符 | |
---|---|
*? | 重复任意次,但尽可能少重复 |
+? | 重复1次或更多次,但尽可能少重复 |
?? | 重复0次或1次,但尽可能少重复 |
{n,m}? | 重复n到m次,但尽可能少重复 |
{n,}? | 重复n次以上,但尽可能少重复 |