网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Java单字符匹配和预定义字符的方法是什么

本篇内容介绍了“Java单字符匹配和预定义字符的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联建站服务项目包括河津网站建设、河津网站制作、河津网页制作以及河津网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,河津网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到河津省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

一、需求

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

  • [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个)

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
    }}

“Java单字符匹配和预定义字符的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


文章题目:Java单字符匹配和预定义字符的方法是什么
分享网址:http://bjjierui.cn/article/iesdpp.html

其他资讯