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
相关推荐
czlczl2002092511 分钟前
OAuth 2.0 解析:后端开发者视角的原理与流程讲解
java·spring boot·后端
颜淡慕潇18 分钟前
Spring Boot 3.3.x、3.4.x、3.5.x 深度对比与演进分析
java·后端·架构
布列瑟农的星空19 分钟前
WebAssembly入门(一)——Emscripten
前端·后端
g***557521 分钟前
Java高级开发进阶教程之系列
java·开发语言
贵州数擎科技有限公司22 分钟前
一批优质 AI 域名转让(.ai)|适合 AI 创业 / 产品 / 公司品牌
前端
小二·28 分钟前
微前端架构完全指南:qiankun 与 Module Federation 双方案深度对比(Vue 3 + TypeScript)
前端·架构·typescript
阿达King哥31 分钟前
在Windows11下编译openjdk 21
java·jvm
EndingCoder1 小时前
枚举类型:常量集合的优雅管理
前端·javascript·typescript
shark-chili1 小时前
从操作系统底层浅谈程序栈的高效性
java
Electrolux1 小时前
[wllama]纯前端实现大语言模型调用:在浏览器里跑 AI 是什么体验。以调用腾讯 HY-MT1.5 混元翻译模型为例
前端·aigc·ai编程