一, 定义
正则表达式(regex)是使用字符串来描述、匹配一系列符合某个句法规则的字符串
用途:匹配、切割、替换、获取字符串
正则表达式由一些普通字符和一些元字符组成。
二、元字符
^匹配输入字符串的开始位置
$匹配输入字符串的结束位置
\d匹配一个数字字符。等价于[0-9]
\D匹配一个非数字字符。等价于[^0-9]
\s匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \n\r\t\f]
\S匹配任何非空白字符。等价于[^\n\r\t\f]
\w匹配包括下划线的任何单个字符。字母数字下划线。等价于"[A-Za-z0-9_]"
\W匹配任何非单个字符。等价于“[^A-Za-z0-9_]”
. 匹配除“\r\n”之外的任何单个字符
{n}n是一个非负整数。匹配确定的n次
{n,}n是一个非负整数。至少匹配n次
{n,m}m和n均为非负整数,其中n<=m。最少匹配n次且最多匹配m次
*匹配前面的子表达式零次或多次(大于等于0次)
?匹配前面的子表达式零次或一次
+匹配前面的子表达式一次或多次(大于等于1次)
三、难点
在Java中反斜线”\”有三种含义:
1. 反斜线后面可以加特定字符,组成所谓的“转义字符”。eg: \n \t
2. 用于取消元字符的意义,使元字符变为普通字符。eg: “\\” 代表”\”。
3. 用于组成正则表达式中的元字符。
eg: “\d” 在正则表达式中代表“匹配一个数字字符”。
package regex;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailRegex {
public static void main(String[] args) {
String emailRegex="\\w+@\\w+\\.\\w+\\.?\\w+";
Scanner scan=new Scanner(System.in);
System.out.print("请输入邮箱:");
String email=scan.next();
Pattern pat=Pattern.compile(emailRegex); // 将正则表达式封装到Pattern对象中
Matcher mat=pat.matcher(email); // 创建一个匹配器Matcher,该Matcher对象可以根据正则表达式判断传入的字符串是否符合要求
if(mat.matches()){
System.out.println("恭喜,邮箱格式正确!");
}else{
System.out.println("对不起,邮箱格式不正确!");
}
}
}
运行结果:
四、Pattern类与Matcher类
1. Pattern类与Matcher类都在java.util.regex包中定义。
2. Pattern类的对象代表正则表达式编译之后的对象;Matcher类主要用于执行验证。
3.Pattern类的主要方法:
public static Pattern compile(String regex);
public Matcher matcher(CharSequence input)
Matcher类的主要方法:
public boolean matches();
find());
group(0); //0是匹配整个正则表达式,1是第一个分组里的
package regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatch {
public static void main(String[] args) {
String birthday="1994-02-25";
String regex="\\d{4}-\\d{2}-\\d{2}";
Pattern pat=Pattern.compile(regex); // 将正则表达式封装到Pattern对象中
Matcher mat=pat.matcher(birthday); // 创建一个匹配器Matcher,该Matcher对象可以根据正则表达式判断传入的字符串是否符合要求
if(mat.matches()){
System.out.println("匹配成功!");
}else{
System.out.println("匹配失败...");
}
}
}
public class GroupRegex {
public static void main(String[] args) {
String str="abchello123regexhello56789javascriptsajdfisjfaslfhello999slakjfldsf";
String regex="\\w+(hello\\d+)\\w+(hello\\d+)";
Pattern pat=Pattern.compile(regex);
Matcher mat=pat.matcher(str);
System.out.println(mat.find());
System.out.println(mat.group(0)); //0是整个串,1是第一个括号里的
System.out.println(mat.group(1));
System.out.println(mat.group(2));
}
运行结果:
五、String类对正则表达式的支持
1. public boolean matches(String regex) 判断字符串是否与给定的正则表达式匹配。
2. public String replaceAll(String regex,String replacement)字符串替换
3. public String[] split(String regex) 字符串拆分
package regex;
public class StringRegex {
public static void main(String[] args) {
String str="hello1java22regex333python4444C55555";
if(str.matches("\\w+")){
System.out.println("匹配成功!");
}
System.out.println("替换后的字符串为:"+str.replaceAll("\\d+","#"));
String country="中国|美国|俄罗斯|德国";
String[] array=country.split("\\|");
for(String c:array){
System.out.print(c+" ");
}
}
}
运行结果:
注意:正则表达式中的问号?有两种作用:
第一种作用:重复前面表达式0次或1次。
第二种作用:在表示次数的元字符后加上?代表取消默认的贪婪匹配模式,变为“非贪婪匹配模式”
String str="abbbbbbb";
System.out.println("贪婪模式匹配: "+str.replaceAll("ab+","#")); // "#"
System.out.println("非贪婪模式匹配: "+str.replaceAll("ab+?","#")); //”#bbbbbb”