正则表达式

常用的正则表达式
[abc] a,b或者c
[^abc] 除了abc
[a-zA-Z]a到z或者A到Z

\d 数字:[0-9]
\D 非数字:[^0-9]
\s 空白字符
\S 非空白字符
\w 单词字符[a-zA-Z_0-9]
\W 非单词字符

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

public class RegexMatches {

    /**
     *X{n,m} X至少出现n次小于m次
     */
    public static void main(String args[]) {
        String str = "123456701";
        String pattern = "[1-9][0-9]{4,14}";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }
    /**
     * X? X,一次或者一次也没有
     */
    @Test
    public void test(){
        String str = "aoob";
        String pattern = "ao?b";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }
    /**
     * 一次或者多次
     */
    @Test
    public void test1(){
        String str = "aoob";
        String pattern = "ao+b";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }
    /**
     * 零次或者多次
     */
    @Test
    public void test2(){
        String str = "ab";
        String pattern = "ao*b";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }
    /**
     * X{n,}至少n次
     */
    @Test
    public void test3(){
        String str = "aoooooooob";
        String pattern = "ao{4,}b";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }
    /**
     * X{4,6}至少4次但是不超过6次
     */
    @Test
    public void test4(){
        String str = "aoooooooooooob";
        String pattern = "ao{4,6}b";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值