Java 正则表达式字母篇

编程入门 行业动态 更新时间:2024-10-14 02:23:44

Java 正则表达式<a href=https://www.elefans.com/category/jswz/34/1765130.html style=字母篇"/>

Java 正则表达式字母篇

字母

匹配 a-z 的小写字母

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

        String regexaz = "[a-z][z-z]";System.out.println("az".matches(regexaz));// trueSystem.out.println("aZ".matches(regexaz));// falseSystem.out.println("AA".matches(regexaz));// falseSystem.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));// trueSystem.out.println("b".matches(regexabc));// trueSystem.out.println("c".matches(regexabc));// trueSystem.out.println("abc".matches(regexabc));// falseSystem.out.println("ad".matches(regexabc));// falseSystem.out.println("abcd".matches(regexabc));// false

匹配非特定的小写字母

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

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

大写字母也可以这样。

匹配 A-Z 的大写字母

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

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

匹配特定的大写字母

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

        String regexRPG = "[RPG]";System.out.println("R".matches(regexRPG));// trueSystem.out.println("P".matches(regexRPG));// trueSystem.out.println("G".matches(regexRPG));// trueSystem.out.println("RPG".matches(regexRPG));// falseSystem.out.println("r".matches(regexRPG));// falseSystem.out.println("p".matches(regexRPG));// falseSystem.out.println("g".matches(regexRPG));// falseSystem.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));// trueSystem.out.println("Az".matches(regexdazAZ));// trueSystem.out.println("az".matches(regexdazAZ));// trueSystem.out.println("11".matches(regexdazAZ));// false

更多推荐

Java 正则表达式字母篇

本文发布于:2023-11-16 17:26:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字母   正则表达式   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!