Java 正则表达式字母篇

字母

匹配 a-z 的小写字母

[a-z] 可以匹配一位 a-z 的小写字母,

        String regexaz = "[a-z][z-z]";
        System.out.println("az".matches(regexaz));// true
        System.out.println("aZ".matches(regexaz));// false
        System.out.println("AA".matches(regexaz));// false
        System.out.println("66".matches(regexaz));// false

匹配特定的小写字母

[abc] 或者 [a-c] 表示只会匹配 a 、b 、c

        //String regexabc = "[abc]";
        String regexabc = "[a-c]";
        System.out.println("a".matches(regexabc));// true
        System.out.println("b".matches(regexabc));// true
        System.out.println("c".matches(regexabc));// true
        System.out.println("abc".matches(regexabc));// false
        System.out.println("ad".matches(regexabc));// false
        System.out.println("abcd".matches(regexabc));// false

匹配非特定的小写字母

和 匹配特定的小写字母 相反,
[^abc] 表示只会匹配 a 、b 、c 以外的字母,

        String regexFabc = "[^abc]";
        System.out.println("a".matches(regexFabc));// false
        System.out.println("b".matches(regexFabc));// false
        System.out.println("c".matches(regexFabc));// false
        System.out.println("d".matches(regexFabc));// true
        System.out.println("z".matches(regexFabc));// true
        System.out.println("x".matches(regexFabc));// true

大写字母也可以这样。

匹配 A-Z 的大写字母

[A-Z] 可以匹配一位 A-Z 的大写字母,

        String regexdAZ = "[A-Z][A-Z]";
        System.out.println("AZ".matches(regexdAZ));// true
        System.out.println("Az".matches(regexdAZ));// false
        System.out.println("az".matches(regexdAZ));// false
        System.out.println("11".matches(regexdAZ));// false

匹配特定的大写字母

只匹配 R 、P 、G ,用 [RPG] ,

        String regexRPG = "[RPG]";
        System.out.println("R".matches(regexRPG));// true
        System.out.println("P".matches(regexRPG));// true
        System.out.println("G".matches(regexRPG));// true
        System.out.println("RPG".matches(regexRPG));// false
        System.out.println("r".matches(regexRPG));// false
        System.out.println("p".matches(regexRPG));// false
        System.out.println("g".matches(regexRPG));// false
        System.out.println("rpg".matches(regexRPG));// false

匹配 a-z 和 A-Z 的任何字母符号

[a-zA-Z] 可以匹配一位 a-z 和 A-Z 的任何字母符号

        String regexdazAZ = "[a-zA-Z][a-zA-Z]";
        System.out.println("AZ".matches(regexdazAZ));// true
        System.out.println("Az".matches(regexdazAZ));// true
        System.out.println("az".matches(regexdazAZ));// true
        System.out.println("11".matches(regexdazAZ));// false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值