文章目录
- 基本介绍
- 元字符
- 转义符\\\\
- 案例代码1
- 案例代码2
- 字符匹配符
- 表格1
- 表格2
- 代码案例(^)
- 代码案例(1.2)
- 代码案例(\\d{3})
- 代码案例(?)
- 代码案例(\\D)
- 代码案例(+)(W)
- 记忆技巧
- 综合代码案例
- 选择匹配符
- 案例练习
- 限定符
- 表格
- 代码案例(n,m)
- 代码案例(+)
- 代码案例(*)
- 代码案例(?)
- 定位符
- 表格
- 代码案例(^)
- 代码案例($)
- 代码案例(\\b)
基本介绍
如果要想灵活的运用正则表达式,必须了解其中各种元字符的功能,元字符从功能上大致分为:
- 限定符
- 选择匹配器
- 分组组合和反向引用符
- 特殊字符
- 字符匹配符
- 定位符
元字符
转义符\\
\\符号
在我们使用正则表达式去检索某些特殊字符的时候,需要用到转义符号,否则检索不到结果,甚至会报错的。
在Java 的正则表达式中,两个\代表其他语言中的一个\
- 需要用到转义符号的字符有以下:.* + () $ /?[]^{}
案例代码1
用"abc$(123(adfasd("去匹配 "("会怎样?
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 12:53
*/
@SuppressWarnings({"all"})
public class RegExp02 {
public static void main(String[] args) {
String content = "abc$(123(adfasd(";
//匹配(
// String regStr = "("; //会报错
String regStr = "\\(";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
(
(
(
案例代码2
在文本中匹配一个.(点)
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 12:53
*/
@SuppressWarnings({"all"})
public class RegExp02 {
public static void main(String[] args) {
String content = "abc.$(123(adfasd(";
//匹配( => \\C
//匹配. => \\.
// String regStr = "("; //会报错
String regStr = "\\.";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
.(注意这里有个点)
字符匹配符
表格1
符号 | 符号 | 示例 | 解释 |
[] | 可接收的字符列表 | [efgh] | e、f、g、h中的任意一个字符 |
[^] | 不接收的字符列表 | [^abc] | 除a、b、c之外的任意一个字符,包括数字和特殊符号 |
- | 连字符 | A-Z | 任意单个大写字母 |
表格2
代码案例(^)
匹配非abc的所有单个字符
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 12:53
*/
@SuppressWarnings({"all"})
public class RegExp02 {
public static void main(String[] args) {
String content = "abc.$(123(adfasd(4563";
String regStr = "[^abc]";
//匹配除了abc的所有单个字符
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
.
$
(
1
2
3
(
d
f
s
d
(
4
5
6
3
代码案例(1.2)
匹配1 * 3
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 12:53
*/
@SuppressWarnings({"all"})
public class RegExp02 {
public static void main(String[] args) {
String content = "abc.$(123(adfasd(";
String regStr = "1.3";
//匹配1 任意字符 3
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
123
代码案例(\d{3})
匹配三个连续数字
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 12:53
*/
@SuppressWarnings({"all"})
public class RegExp02 {
public static void main(String[] args) {
String content = "abc.$(123(adfasd(";
// String regStr = "\\d\\d\\d";
String regStr = "\\d{3}";
//匹配连续三个数字
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
123
代码案例(?)
匹配3或4个连续数字
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 12:53
*/
@SuppressWarnings({"all"})
public class RegExp02 {
public static void main(String[] args) {
String content = "abc.$(123(adfasd(4563";
String regStr = "\\d{3}(\\d)?"; //匹配3或4个数字
//(\\d)? 匹配0-1个数字 ?代表.0-1
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
123 4563
代码案例(\D)
匹配单个非数字字符开头,后接任意个数字字符串
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 12:53
*/
@SuppressWarnings({"all"})
public class RegExp02 {
public static void main(String[] args) {
String content = "abc.$(123(adfasd(4563";
String regStr = "\\D(\\d)*";
//匹配非数字,任意一个字符
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
a
b
c
.
$
(123
(
a
d
f
a
s
d
(4563
代码案例(+)(W)
以至少一个非数字字母字符开头,两个数字字符结尾的字符串
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 12:53
*/
@SuppressWarnings({"all"})
public class RegExp02 {
public static void main(String[] args) {
String content = "abc.$(123(adfasd(4563";
String regStr = "\\W+\\d{2}";
//以至少一个非数字字母字符开头,两个数字字符结尾的字符串
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
.$(12
(45
记忆技巧
关于正则表达式的“d”匹配符:一个\d代表匹配一个数字,而“d”正是 digit英文的缩写,译为:数字
而正则表达式的"w",他所匹配的是任意的数字或大小写字母的字符,也正是word单词的缩写
综合代码案例
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*Create By 刘鸿涛
*2022/3/8 17:31
*/
@SuppressWarnings({"all"})
public class RegExp03 {
public static void main(String[] args) {
String content = "a11c8abc_ABC";
// String regStr = "[a-z]"; //匹配a-z任意一个字符
// String regStr = "[A-Z]"; //匹配A-Z任意一个大写字符
// String regStr = "abc"; //匹配 abc 字符串 [默认区分大小写]
// String regStr = "(?i)abc"; //匹配 abc 字符串[不区分大小写]
// String regStr = "a(?i)bc"; //匹配 abc 字符串,其中bc不区分大小写
// String regStr = "a((?i)b)c"; //匹配 abc 字符串,其中b不区分大小写
//还有一个不区分大小写的语句写法,在创建Pattern对象时,指定CASE_INSENSITIVE 参数
// Pattern pattern = Pattern.compile(regStr, Pattern.CASE_INSENSITIVE);
// String regStr = "[^a-z]"; //匹配 不再 a-z之间任意一个字符
// String regStr = "[abcd]"; //匹配 在 abcd 中任意一个字符
String regStr = "\\w"; //匹配 字母,数字,下划线
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
选择匹配符
在匹配某个字符串的时候是选择性的,即:既可以匹配这个,又可以匹配那个,这时你需要用到 选择匹配符号 |
符号 | 符号 | 示例 | 解释 |
| | 匹配"|"之前或之后的表达式 | ab|cd | ab或者cd |
案例练习
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 18:34
*/
@SuppressWarnings("all")
public class RegExp04 {
public static void main(String[] args) {
String content = "taotao 涛 刘绦";
String regStr = "tao|涛|绦";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group());
}
}
}
tao
tao
涛
绦
限定符
用于指定其前面的字符和组合项连续出现多少次
表格
代码案例(n,m)
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:06
* 演示限定符的使用
*/
@SuppressWarnings({"all"})
public class RegExp05 {
public static void main(String[] args) {
String content = "1111111faaaaaahello";
// String regStr = "1{4}"; //表示匹配1111
// String regStr = "\\d{2}"; //表示匹配 两位的任意数字字符
// String regStr = "a{3,4}"; //表示匹配 aaa 或者 aaaa
//细节:java匹配默认贪婪匹配,即尽可能匹配多的
String regStr = "1{4,5}"; //表示匹配1111 或 11111
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("找到" + matcher.group(0));
}
}
}
找到11111
代码案例(+)
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:06
* 演示限定符的使用
*/
@SuppressWarnings({"all"})
public class RegExp05 {
public static void main(String[] args) {
String content = "1111111faaaaaahello";
String regStr = "1+"; //表示匹配1到多个1
//1111111
//依然是贪婪匹配
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("找到" + matcher.group(0));
}
}
}
找到1111111
代码案例(*)
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:06
* 演示限定符的使用
*/
@SuppressWarnings({"all"})
public class RegExp05 {
public static void main(String[] args) {
String content = "1111111faaaaaahello";
String regStr = "1*"; //表示匹配零个1到多个1
//找到1111111
//找到
//找到
//找到
//找到
//...
//贪婪匹配
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("找到" + matcher.group(0));
}
}
}
找到1111111
找到
找到
找到
找到
找到
找到
找到
找到
找到
找到
找到
找到
找到
代码案例(?)
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:06
* 演示限定符的使用
*/
@SuppressWarnings({"all"})
public class RegExp05 {
public static void main(String[] args) {
String content = "1111111faaaaaahello";
String regStr = "a1?"; //表示匹配 a 或者 a1
//因为?代表 0 - 1 ,代表true或false的意思
//所以匹配0个1或1个1
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
System.out.println("找到" + matcher.group(0));
}
}
}
找到a
找到a
找到a
找到a
找到a
找到a
定位符
定位符,规定要匹配的字符串出现的位置,比如在字符串的开始还是在结束的位置,这个也是相当有用的,必须掌握
表格
代码案例(^)
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:57
*/
@SuppressWarnings({"all"})
public class RegExp06 {
public static void main(String[] args) {
String content = "123abc";
//以至少一个数字开头,后接任意个小写字母的字符串
String regStr = "^[0-9]+[a-z]*";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
123abc
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:57
*/
@SuppressWarnings({"all"})
public class RegExp06 {
public static void main(String[] args) {
String content = "a123abc";
//以至少一个数字开头,后接任意个小写字母的字符串
String regStr = "^[0-9]+[a-z]*";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
空
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:57
*/
@SuppressWarnings({"all"})
public class RegExp06 {
public static void main(String[] args) {
String content = "123abc12";
//以至少一个数字开头,后接任意个小写字母的字符串
String regStr = "^[0-9]+[a-z]*";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
123abc
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:57
*/
@SuppressWarnings({"all"})
public class RegExp06 {
public static void main(String[] args) {
String content = "123";
//以至少一个数字开头,后接任意个小写字母的字符串
String regStr = "^[0-9]+[a-z]*";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
123
代码案例($)
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:57
*/
@SuppressWarnings({"all"})
public class RegExp06 {
public static void main(String[] args) {
String content = "123abc";
//以至少一个数字开头,必须以至少一个小写字母结束
String regStr = "^[0-9]+[a-z]+$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
123abc
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:57
*/
@SuppressWarnings({"all"})
public class RegExp06 {
public static void main(String[] args) {
String content = "123-abc";
//以至少一个数字开头,必须以至少一个小写字母结束,中间还必须有-
String regStr = "^[0-9]+\\-[a-z]+$";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
123-abc
代码案例(\b)
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:57
*/
@SuppressWarnings({"all"})
public class RegExp06 {
public static void main(String[] args) {
String content = "taoliuhong liutao tao";
String regStr = "tao\\b";
//表示只匹配边界的tao,也就是右边两个【这里的边界是指:被匹配的字符串最后,
//也可以是空格的子字符串的后面】
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
tao
tao
package com.taotao.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Create By 刘鸿涛
* 2022/3/8 20:57
*/
@SuppressWarnings({"all"})
public class RegExp06 {
public static void main(String[] args) {
String content = "taoliuhong liutao tao";
String regStr = "tao\\b";
//匹配的左边的tao
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
}
}
}
tao