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
相关推荐
嘟嘟可在哪里。1 分钟前
IntelliJ IDEA git凭据帮助程序
java·git·intellij-idea
岁忧2 分钟前
(LeetCode 每日一题) 3541. 找到频率最高的元音和辅音 (哈希表)
java·c++·算法·leetcode·go·散列表
_extraordinary_6 分钟前
Java 多线程进阶(四)-- 锁策略,CAS,synchronized的原理,JUC当中常见的类
java·开发语言
懒大王95276 分钟前
uni-app + Vue3 开发展示 echarts 图表
前端·uni-app·echarts
yinuo13 分钟前
Uni-App跨端实战:微信小程序WebView与H5通信全流程解析(01)
前端
xkroy31 分钟前
ajax
前端·javascript·ajax
Yvonne爱编码35 分钟前
AJAX入门-URL、参数查询、案例查询
前端·javascript·ajax
闲人编程44 分钟前
前端形态与样式风格:从古典到现代的视觉语言演进
前端·css·状态模式·组件·js·风格·响应式
JudithHuang44 分钟前
Mac版微信开发者工具登录二维码不显示问题解决方案
前端
纪元A梦1 小时前
贪心算法应用:信用评分分箱问题详解
java·算法·贪心算法