正则表达式

概述

正则表达式:正确规则的表达式 规则是java给我们定的
是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。

使用

规则字符在java.util.regex Pattern类中
1.字符
x 字符 x。举例:‘a’表示字符a
\ 反斜线字符。
\n 新行(换行)符 (’\u000A’)
\r 回车符 (’\u000D’)

//这是最简单的正则表达式
String regx="a";
String s="a";
//matches方法用于判断字符串是否匹配正则表达式,返回一个boolean类型的值
boolean b=s.matches(regx);//true

2.字符类
[abc] a、b 或 c(简单类)
[^abc]任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括

//表示字符串只能含有a、b、c之中的字符
String regx="[abc]";
String s1="a";
boolean b1=s1.matches(regx);//true
String s2="d";
boolean b2=s2.matches(regx);//false
//字符串中的字符不为数字
String regx="[^0-9]";
String s1="3";
boolean b1=s1.matches(regx);//false
String s2="A";
boolean b2=s2.matches(regx);//true
//字符串中的字符为小写字母或数字
String regx="[a-z0-9]";
String s="3";
boolean b=s.matches(regx);

3.预定义字符类
. 任何字符。如果想要表示".",需要用"."表示
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成
\s 匹配空格字符

//字符为空格
String regx="\\s";
String s=" ";
boolean b=s.matches(regx);//true

4.边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi
5Greedy 数量词
X? X:一次或一次也没有,比如""空串 就是没有
X* X:零次或多次 ,大于等于1次都算多次
X+ X:一次或多次
X{n} X:恰好 n 次
X{n,} X:至少 n 次
X{n,m} X:至少 n 次,但是不超过 m 次

//数字刚好出现3次
String regx="[0-9]{3}";
String s1="123";
boolean b1=s1.matches(regx);//true
String s2="12";
boolean b2=s2.matches(regx);//false
//小写字母出现2到5次
String regx="[a-z]{2,5}";
String s1="abcde";
boolean b1=s1.matches(regx);
String s2="abcdefg";
boolean b2=s2.matches(regx);
//数字出现1次或多次
String regx="[0-9]+";
String s="123456";
boolean b=s.matches(regx);//true

练习:

/*
检验手机号
手机号由11位数字组成
第一个数字为1,第二个数字为3,5,6,7,8,9
*/
String regx="[1][^124][0-9]{9}";

分割方法
String类的方法:public String[] split(String regex)
将此字符串拆分为给定的regular expression的匹配。

public class Demo3 {
    public static void main(String[] args) {
        String regx=" ";
        String s1="ab cd ef g";
        //以空格将字符串分割开,形成字符串数组
        String[] s=s1.split(regx);
        System.out.println(Arrays.toString(s));
    }
}
/*
运行结果:
[ab, cd, ef, g]
*/

替换方法
String类的方法:public String replaceAll(String regex,String replacement)
用给定的字符串替换与给定的regular expression匹配的此字符串的每个子字符串。

public class Demo {
    public static void main(String[] args) {
        String regx=" ";
        String s1="ab cd ef g";
        String s=s1.replaceAll(regx,",");
        System.out.println(s);
    }
}
/*
运行结果:
ab,cd,ef,g
*/
Pattern和Matcher的概述

Pattern(模式):必须首先将正则表达式(指定为字符串)编译为此类的实例。 然后将所得的图案可以被用来创建一个Matcher对象可以匹配任意character sequences针对正则表达式。
Matcher(匹配器):执行上匹配操作的引擎character sequence通过解释Pattern 。

Pattern p=Pattern.compile("[0-9]{5}");
Matcher m=p.matcher("12345");
boolean b=m.matches();		//true

public boolean find()
尝试找到匹配模式的输入序列的下一个子序列。
public String group()
返回与上一个匹配匹配的输入子序列。

//获取下面这个字符串中由三个字符组成的单词
//da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?
public class Demo3 {
    public static void main(String[] args) {
        String str="da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";
        Pattern p=Pattern.compile("\\b[a-z]{3}\\b");
        Matcher m=p.matcher(str);
        while (m.find())
        {
            String group=m.group();
            System.out.print(group+" ");
        }
    }
}
/*
运行结果:
jia jin yao xia wan gao 
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值