当前位置: 代码网 > it编程>编程语言>正则表达式 > Java 中的正则表达式单字符预定义字符匹配问题

Java 中的正则表达式单字符预定义字符匹配问题

2024年05月18日 正则表达式 我要评论
一、需求❓ 现有一个字符串,需满足如下要求:① [6, 18] 个字符② 只能包含字母、数字、下划线③ 需以字母开头❓ 如果字符串满足上述要求,返回 true,否则返回 falsepublic sta

一、需求

❓ 现有一个字符串,需满足如下要求:
[6, 18] 个字符
② 只能包含字母、数字、下划线
③ 需以字母开头
❓ 如果字符串满足上述要求,返回 true,否则返回 false

public static boolean validstring(string s) {
       return s.matches("[a-za-z][a-za-z0-9_]{5,17}");
   }

🍀 正则表达式用极简的规则取代了复杂的验证逻辑
🍀 regex expression
🍀 正则表达式是一种通用的技术,适用于多种编程语言

二、单字符匹配(6个)

✏️ ① [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 
    }
}

✏️ ② [^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
    }
}

✏️ ③ [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
    }
}

✏️ ④ [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
    }
}

✏️ ⑤ [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
    }
}

✏️ ⑥ [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
    }
}

到此这篇关于java 中的正则表达式(单字符匹配和预定义字符)的文章就介绍到这了,更多相关java正则表达内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com