String类---正则表达式

正则表达式了解

正则表达式

一、字符
[a-z] // 匹配所有的小写字母
[A-Z] // 匹配所有的大写字母
[a-zA-Z] // 匹配所有的字母
[0-9] // 匹配所有的数字
[ \f\r\t\n] // 匹配所有的白字符
说明:特殊字符 .(点,句号)在正则表达式中用来表示除了"新行"之外的所有字符

二、数字类
[0-9]:字符串这个位置只能是数字
[^0-9]:字符串这个位置不能是数字
[\d]:等同于[0-9]
[\D]:等同于[^0-9]
三、数量词
x{m} :x字符或字符簇只出现m次

//例:a{2}表示a只出现两次

x{m,}:x字符出现m或更多的次数
x{m,n}:前面的内容至少出现m次,但不超过n次
x? :x这个字符出现一次或一次也没有
x* :x这个字符出现零次或多次
x+:x这个字符出现至少一次
四、预定义字符
[\w]:文字字符,包含字母,数字,下划线[a-zA-Z0-9_]
[\W]:文字字符,不能包含字母,数字,下划线[^a-zA-Z0-9_]
五、正则表达式的应用
验证QQ邮箱

public class ZhengZeText {
    public static void main(String[] args) {
        ZhengZeText text = new ZhengZeText();
        text.method1();
    }

    public void method1(){
        String atr = "1342328788@qq.com";
        //正则规则
        String ze = "[\\w]+@[a-z]+\\.[a-z]+";
        boolean a = atr.matches(ze);
        System.out.println(a);
    }
}

拆分字符串

public class SplitText {
    public static void main(String[] args) {
        SplitText text = new SplitText();
        text.method1();

    }
    
    public void method1(){
        String str = "aj1sg2fi34u54a";
        String[] strings = str.split("[\\d]+");
        for (int i = 0;i<strings.length;i++){
            System.out.println(strings[i]);
        }

    }
}

替换字符串中的字符
将字符串中的数字转换成@符号

public class ReplaceText {
    public static void main(String[] args) {
        ReplaceText text = new ReplaceText();
        text.method2();

    }

    public void method2() {
        String str = "kjd54gf6asj78d5gf";
        String restring = "[\\d]";
        String re = str.replaceAll("[\\d]","@");
        System.out.println(re);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值