正则呀正则

import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;

import java.util.ArrayList;
import java.util.Arrays;

public class demo {

    public static void main(String[] args) {
        String s1;
        boolean r;

        //匹配10位 以2开头第二位是0,第三位是1 后七位位任意字符
        s1= "2016564726";
        r = s1.matches("2[0][1]\\d{7}");
        System.out.println(r);

        //匹配至少4个小写字母
        s1 = "woshizhangdeshuai";
        r = s1.matches("[a-z]{4,}");
        System.out.println(r);

        //匹配三个a
        s1="aaa";
        r = s1.matches("a{3}");
        System.out.println(r);

        //匹配至少三个a
        s1 = "aaaa";
        r = s1.matches("a{3,}");
        System.out.println(r);

        //匹配数字字母下划线
        s1 = "_";  // asd   _  7885
        r = s1.matches("[a-zA-Z_0-9]");
        System.out.println(r);

        //非 的表达 (表示非a 非f)
        s1 = "a";
        r = s1.matches("[^af]");
        System.out.println(r);

        //. 任意字符
        s1 ="s";
        r = s1.matches(".");
        System.out.println(r);

        //  \.真正的.
        s1 = ".";
        r = s1.matches("\\.");
        System.out.println(r);

        // \w 单词字符
        s1 ="a";
        r = s1.matches("\\w");
        System.out.println(r);

        // \W 非单词字符
        s1 = "*";
        r = s1.matches("\\W");
        System.out.println(r);

        //  \d  数字
        s1 = "2";
        r = s1.matches("\\d");
        System.out.println(r);

        // \D 非数字
        s1 = "*";
        r = s1.matches("\\D");
        System.out.println(r);


        //  \s 空白字符串  空格  \t \r \n
        s1 = " ";
        r = s1.matches("\\s");
        System.out.println(r);

        // \S 非空白
        s1 = "*";
        r = s1.matches("\\S");
        System.out.println(r);

        //  ^ 行开头
        s1 = "a";
        r = s1.matches("^[a]");
        System.out.println(r);

        // $行结尾
        s1 = "f";
        r = s1.matches("[f$]");
        System.out.println(r);

        //匹配邮件
        s1 = "2239517047@qq.com";
        r = s1.matches("[\\d]{7,10}@\\S+\\.(com|cn|com\\.cn|edu|org)");
        System.out.println(r);

        /*
            字符串有4个方法,可以直接使用正则
                    matches
            replaceAll
                    replaceFirst
            split
        */
        s1 = "aadfjo477df5a7s7d7aa5df7fad8";
        String s = s1.replaceAll("\\D+", "!");
        System.out.println(s);

        s1 = "aadfjo477df5a7s7d7aa5df7fad8";
        s = s1.replaceAll("\\d+", "!");
        System.out.println(s);

        s1 = "大大家家家好";
        s = s1.replaceAll("(.)\\1+", "$1");
        System.out.println(s);

        // ip 切割
        s1 = ".192.168.1.102";
        String[] s2 = s1.split("\\.");
        System.out.println(Arrays.toString(s2));
        
        s1 = "192..168.1.102";
         s2 = s1.split("\\.");
        System.out.println(Arrays.toString(s2));

        s1 = "192.168.1.102.";
       s2 = s1.split("\\.");
        System.out.println(Arrays.toString(s2));

        s1 = "192.168.1.102";
        s2 = s1.split("\\.");
        System.out.println(Arrays.toString(s2));

    }
}


/*
正则表达式:
        是一个文本处理工具,用来匹配文本,替换文本
捕获组
( )
贪婪模式(默认的)
懒惰模式
        量词后面加?

数量词:
a{m}  m个a
a{m,} 至少m个a
a{m,n} 至少m个a,至多n个a
a*   0个或多个  a{0,}
a+   至少一个   a{1,}
a?   0个或1个  a{0,1}

如何写正则表达式:
[abc]   或a或b或c
[a-z]   所有小写字母
[a-zA-Z_0-9]  数字字母下划线
[^ac]如果^再[]内部的时候表示非   非a非c
. 任意字符(不包括 \r \n)
\.  真正的 .
\w  w:word 单词字符 [a-zA-Z_0-9]
\W  w:word 非单词字符 [^a-zA-Z_0-9]
\d  d:digital  数字  [0-9]
\D  非数字
\s  s:space 空白字符串  空格  \t  \r  \n
\S  非空白
^   行开头
$   行结尾

--------------
Pattern
     写模式
matcher
      匹配器
---------------
字符串有4个方法,可以直接使用正则
    matches
    replaceAll
    replaceFirst
    split
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值