详细介绍Java正则表达式之单字符匹配和预定义字符

2023-01-21 0 3,130

一、需求

现有一个字符串,需满足如下要求:

如果字符串满足上述要求,返回 true,否则返回 false

   public static boolean validString(String s) {
       return s.matches("[a-zA-Z][a-zA-Z0-9_]{5,17}");
   }

正则表达式用极简的规则取代了复杂的验证逻辑

Regex Expression

正则表达式是一种通用的技术,适用于多种编程语言

二、单字符匹配(6个)

1. [abc]:字符串的某个位置(某一个字符)满足 a、b、c 中的一个

某个位置:该【单字符匹配】放的位置

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[zgq]";
        System.out.println("z".matches(regex)); // true
        System.out.println("g".matches(regex)); // true
        System.out.println("q".matches(regex)); // true
        System.out.println("zgq".matches(regex)); // false
    }}
public class TestDemo {
    public static void main(String[] args) {
        String regex = "26[abc]3q";
        System.out.println("26a3q".matches(regex)); // true
        System.out.println("26abc".matches(regex)); // false
        System.out.println("26b3q".matches(regex)); // true 
    }}

2. [^abc]:除了 a、b、c 之外的任意单个字符

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[^cat]666";
        System.out.println("c666".matches(regex)); // false
        System.out.println("a666".matches(regex)); // false
        System.out.println("t666".matches(regex)); // false
        System.out.println("bb666".matches(regex)); // false
        System.out.println("b666".matches(regex)); // true
    }}
public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[12345]666";
        String regex2 = "[^1-5]666";
        System.out.println("1666".matches(regex1)); // true
        System.out.println("3666".matches(regex1)); // true
        System.out.println("5666".matches(regex1)); // true
        System.out.println("6666".matches(regex1)); // false

        System.out.println("1666".matches(regex2)); // false
        System.out.println("3666".matches(regex2)); // false
        System.out.println("5666".matches(regex2)); // false

        System.out.println("6666".matches(regex2)); // true
    }}

3. [a-zA-z]:匹配单个英文字母

public class TestDemo {
    public static void main(String[] args) {
        String regex = "[a-zA-Z]666";
        System.out.println("6666".matches(regex)); // false
        System.out.println("b666".matches(regex)); // true
    }}

4. [a-d[1-6]]:和 [a-d1-6] 一样(并集)

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[a-d[1-6]]";
        String regex2 = "[a-d1-6]";
        System.out.println("a".matches(regex1)); // true
        System.out.println("e".matches(regex1)); // false
        System.out.println("1".matches(regex1)); // true
        System.out.println("7".matches(regex1)); // false

        System.out.println("a".matches(regex2)); // true
        System.out.println("e".matches(regex2)); // false
        System.out.println("1".matches(regex2)); // true
        System.out.println("7".matches(regex2)); // false
    }}

5. [zgq&&[god]]:交集

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[zgq&&[god]]";
        System.out.println("q".matches(regex1)); // false
        System.out.println("d".matches(regex1)); // false
        System.out.println("g".matches(regex1)); // true
    }}

6. [zgq&&[god]]:取差集

public class TestDemo {
    public static void main(String[] args) {
        String regex1 = "[zgq&&[^god]]";
        System.out.println("q".matches(regex1)); // true
        System.out.println("d".matches(regex1)); // false
        System.out.println("g".matches(regex1)); // false
        System.out.println("z".matches(regex1)); // true

        // 取差集, 从字母 a 到字母 z 中去除字母 b 和 d
        String regex2 = "[a-z&&[^bd]]";
        System.out.println("d".matches(regex2)); // false
        System.out.println("a".matches(regex2)); // true
    }}

三、预定义字符(7个)

预定义字符匹配的仍然是单个字符

.】:任意单个字符
\\d】:数字
\\D】:非数字
\\s】:空白
\\S】:非空白
\\w】:字母(英文字母、下划线、数字)
\\W】:非英文字母

Java 中需以两个【\\】开头表示预定义字符

public class TestDemo {
    public static void main(String[] args) {
        String r1 = ".";
        System.out.println("@".matches(r1)); // true
        System.out.println("庆".matches(r1)); // true
        System.out.println("I".matches(r1)); // true
        System.out.println(" ".matches(r1)); // true
        System.out.println(".".matches(r1)); // true
    }}
public class TestDemo {
    public static void main(String[] args) {
        // 匹配 java 文件
        String r1 = ".\\\\.java";
        System.out.println("a.java".matches(r1)); // true
        System.out.println("xjava".matches(r1)); // false
        System.out.println("5java".matches(r1)); // false
    }}
public class TestDemo {
    public static void main(String[] args) {
        String r1 = "[abc]";
        String r2 = "\\\\[abc\\\\]";
        System.out.println("a".matches(r1)); // true
        System.out.println("c".matches(r1)); // true
        System.out.println("[abc]".matches(r1)); // false

        System.out.println("a".matches(r2)); // false
        System.out.println("c".matches(r2)); // false
        System.out.println("[abc]".matches(r2)); // true
    }}

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 详细介绍Java正则表达式之单字符匹配和预定义字符 http://www.20zxx.cn/463003/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务