正则表达式:
1.常见的语法:A:字符
x字符 举例:a代表'a'
\\ 反斜线字符 代码中:两个反斜线代表一个反斜线
\t 制表符
\n 换行符
\r 回车符
B:字符类
[abc] a、b、或c
[^abc] 任何字符,除了 a、b、或c(否定)
[a-zA-Z] a到z或A到Z,两头的字母包括在内(范围)
C:预定义字符类
. 邮箱2041687917@qq.com:如果本身表示的就是一个.怎么办?\.表示点(代码中写正则表达式的时候//.)
\d 数字[0-9]
\w 单词字符:[a-zA-Z_0-9]:举例邮箱:zhangsan_isSone213824932@.163.com
D:边界匹配器
.\b 单词边界 hello;world;haha;xixi
^ 以什么开头 ^0以0开头
$ 以什么结尾
E:Greedy数量词(重点)
x? x出现了1次或者是0次
x* x出现了0次或者是多次
x+ x出现了1次或多次
x{n} x出现了恰好n次
x{n,} x出现了至少n次
x{n,m} x至少n次,不超过m次
代码:需求录入一个QQ号码,检验QQ号码
//规则QQ号码5-15位组成的一个数字
//并且不能以0开头
import java.util.Scanner;
public class RegexDemo {
public static <teturn> void main(String[] args) {
//键盘录入对象
Scanner sc=new Scanner(System.in);
//接收并录入数据
System.out.println("请您输入您的QQ号码");
String qq=sc.nextLine();
//调用方法一
boolean flag=cheakQQ(qq);
System.out.println(flag);
//调用方法二
boolean flag1=cheakQQ1(qq);
System.out.println(flag1);
//调用方法三
boolean flag2=cheakQQ2(qq);
System.out.println(flag2);
}
//写一个功能
//方法一:正则表达式链式编程
public static boolean cheakQQ1(String qq){
return qq.matches("[1-9]\\d{4,14}");
}
public static boolean cheakQQ(String qq){
//方法二:使用正则表达式
//给qq定义一个规则
String regex="[1-9][0-9]{4,14}";
boolean flag=qq.matches(regex);
return flag;
}
//方法三:普通方法
public static boolean cheakQQ2(String qq){
boolean flag=true;
//满足条件
if(qq.length()>=5&&qq.length()<=15){
if(!qq.startsWith("0")){
//将字符串转化成数组判断是否是数字
char[]chs=qq.toCharArray();
//遍历数组获取每一个元素
for(int i=0;i<chs.length;i++){
char ch=chs[i];
if(!Character.isDigit(ch)){
flag=false;
break;
}
}
}else{
flag=false;
}
}else{
flag=false;
}
return flag;
}
}
练习1:
import java.util.Scanner;
//检验手机号
public class RegexDemo2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请您输入一个手机号");
String phone=sc.nextLine();
//使用正则表达式
//使用一个功能
boolean flag=cheakPhone(phone);
System.out.println(flag);
}
public static boolean cheakPhone(String phone){
return phone.matches("[1][3578]\\d{9}");
//matches方法是用户录入的字符串和定义正则表达式进行匹配,匹配不成功,返回false
}
}
2.正则表达式String类中的一个方法
切割方法:
public SString[] split(String regex):根据正则表达式的匹配拆分此字符串
代码:
import java.util.Arrays;
/*
* 我有如下一个字符串"32,45,56,768,54"
* 请用代码实现最终结果:32 45 54 56 768
*/
public class RegexTest {
public static void main(String[] args) {
String s="32,45,56,768,54";
//使用切割功能进行拆分
String[] strArray=s.split(",");
//创建int数组,使用静态初始化
//将String类型的数组转化为int数组,
int []arr=new int[strArray.length];
//遍历数组
for(int x=0;x<strArray.length;x++){
arr[x]=Integer.parseInt(strArray[x]);
}
//对数组进行排序
Arrays.sort(arr);
//将排好序的数组组装成一个字符串类型
//创建字符串缓冲区
StringBuilder sb=new StringBuilder();
//遍历数组
for(int x=0;x<arr.length;x++){
sb.append(arr[x]).append(" ");
}
//将StringBuilder转化成字符串类型
String result=sb.toString().trim();
System.out.println(result);
}
3.String类中另一个方法和正则表达式有关系
public String replaceAll(String regex,String replacement)
使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串
代码:
public class RegexDemo3 {
public static void main(String[] args) {
//定义一个字符串
String s="wihiwjdiwh928740927493ncjskhcks";
//定义一个正则表达式
String regex="\\d+";
//定义一个新的字符串
String ss="****";
//使用给定的replacement替换
String result=s.replaceAll(regex, ss);
System.out.println(result);
}
}
4.关于模式和匹配器使用的基本顺序
1)模式和匹配器应用的典型顺序:将正则表达式编译成模式对象:Pattern
public static Pattern compile(String regex);
2)使用模式对象中的一些功能,来和传入的字符串进行匹配
public Matcher matcher(String str);
通过模式对象传入指定的字符串来获得一个匹配器对象
3)再去使用匹配器对象中的一些方法,判断我们给定的字符串是满足正则规则
****************但是通过Pattern模式类中的compile()要获取模式对象,
通过里面的matcher()方法获取匹配对象器Matcher,
再去使用匹配对象器中的matches()进行匹配,最终返回boolean,一般情况不这样使用
代码:
public class RedexDemo4 {
public static void main(String[] args) {
Pattern p=Pattern.compile("d+b");
Matcher m=p.matcher("dddddddddddb");
boolean flag=m.matches();
System.out.println(flag);
//上述方式进行正则检验非常麻烦啊
//改进
//定义正则规则
String regex="d+b";
//定义一个字符串
String s="dddddddddddb";
boolean flag2=s.matches(regex);
System.out.println(flag2);