正则表达式

本质就是一个字符串,可以指定一些规则,来校验其他字符串。

格式:

String telRegex="编写规则"

“需要做校验的字符串”.matches(telRegex));

字符类:

[abc]                                   只能是a,b,c

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "[abc]";
        System.out.println("jby".matches(regex));

    }
}

[^abc]                                 除了a,b,c之外的任何字符

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "[^abc]";
        System.out.println("jby".matches(regex));
    }
}

[a-zA-Z]                              a到在  A到Z,包括(范围)

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "[a-zA-Z]";
        System.out.println("jby".matches(regex));
    }
}

[a-d[m-p]]                           a到d,或m通过p:([a-dm-p]联合)

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "[a-d[m-p]]";
        System.out.println("jby".matches(regex));
    }
}

[a-z&&[def]]                        d,e,或f(交集)

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "[a-z&&[def]]";
        System.out.println("jby".matches(regex));
    }
}

[a-z&&[^bc]]                       a到z,除了b和c:([ad-z]减法)

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "[a-z&&[^bc]]";
        System.out.println("jby".matches(regex));
    }
}

[a-z&&[^m-p]]                     a到z,除了m到p:([a-lq-z])

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "[a-z&&[^m-p]]";
        System.out.println("jby".matches(regex));
    }
}

预定义字符类

.                                                     任何字符

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = ".";
        System.out.println("jby".matches(regex));
    }
}

\d                                                   一个数字:[0-9]

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\d";
        System.out.println("jby".matches(regex));
    }
}

\D                                                   非数字:[^0-9]

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\D";
        System.out.println("jby".matches(regex));
    }
}

\s                                                    一个空白字符:[  \t\n\x0B\f\r]

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\s ";
        System.out.println("jby".matches(regex));
    }
}

\S                                                   非空白字符:[^\s]

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\S";
        System.out.println("jby".matches(regex));
    }
}

\w                                                   [a-zA-Z_0-9]:英文、数字、下划线

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\w";
        System.out.println("jby".matches(regex));
    }
}

\W                                                  [^\w]:一个非单词字符

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\W";
        System.out.println("jby".matches(regex));
    }
}

数量

X?                                          一次或0次

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\W?";
        System.out.println("jby".matches(regex));
    }
}

X*                                           零次或多次

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\W*";
        System.out.println("jby".matches(regex));
    }
}

X+                                          一次或多次

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\W+";
        System.out.println("jby".matches(regex));
    }
}

X{n}                                        正好n次 

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\W{8}";
        System.out.println("jby".matches(regex));
    }
}

X{n,  }                                     至少n次

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\W{8,}";
        System.out.println("jby".matches(regex));
    }
}

X{n,m}                                    至少n次但不超过m次

package com.bingyi.regex;

public class RegexDemo {
    public static void main(String[] args) {
        String regex = "\\W{8,11}";
        System.out.println("jby".matches(regex));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰逸.itbignyi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值