在Java中使用正则表达式温习笔记

##一、正则表达式概念


正则表达式Regex(Regular Expression),是一种通过定义由特定字符组成的表达式来对字符串进行匹配、查找、替换和切割的字符串操作工具。


##二、正则表达式中特定的一些字符


###1.元字符

这里写图片描述


###2.限定字符表

这里写图片描述


###3.方括号中的含义表

这里写图片描述


##三、使用正则表达式对字符串进行匹配


###1.使用正则表达式对字符串进行匹配有三种方式:

1)使用字符串的对象的matches()方法
2)使用Matcher的对象的matches()方法
3)使用Pattern类的matches()方法


###2.匹配的特点:

1)根据正则表达式的规则对整个字符串进行匹配
2)匹配结果返回对应的布尔值

public class MatchTest {
	public static void main(String[] args) {
            System.out.println("一,使用字符串的对象的matches()方法");
            matchDemo_1("13805646681");
            matchDemo_1("03805646681");
            matchDemo_1("13805Jia681");
            matchDemo_1("138056");
            System.out.println("二,使用Matcher的对象的matches()方法");
            matchDemo_2("13805646681");
            matchDemo_2("03805646681");
            matchDemo_2("13805Jia681");
            matchDemo_2("138056");
            System.out.println("三,使用Pattern类的matches()方法");
            matchDemo_3("13805646681");
            matchDemo_3("03805646681");
            matchDemo_3("13805Jia681");
            matchDemo_3("138056");
        }

        public static void matchDemo_1(String str) {
            String regex = "1\\d{10}";
            boolean isMatched = str.matches(regex);
            System.out.println(str + (isMatched ? "是电话号码" : "不是电话号码"));
        }

        public static void matchDemo_2(String str) {
            String regex = "1[0-9]{10}";
            //1,将正则表达式编译成Pattern对象 
            Pattern p = Pattern.compile(regex); 
            // 2, 与字符串进行关联,生成Matcher对象 
            Matcher m = p.matcher(str); 
            // 3,对字符串进行操作 
            boolean isMatched = m.matches(); 
            System.out.println(str + (isMatched ? "是电话号码" : "不是电话号码")); 
        } 
        
        public static void matchDemo_3(String str) {
            String regex = "1\\d{10}";
            boolean isMatched = Pattern.matches(regex, str); 
            System.out.println(str + (isMatched ? "是电话号码" : "不是电话号码")); 
        } 
}

###3.使用正则表达式对字符串进行替换

正则表达式既可以替换字符串中所有匹配到的字符,也可以只替换第一次匹配到的字符,对应的两个方法是replaceALL和replaceFirst,同样在String对象和Matcher对象中都包含这两个方法。
替换的特点:
1)可以将正则表达式匹配到的字符(串)替换为你指定的字符(串)
2)替换结果生成新的字符串

class StackedWordsReplace {

    public static void main(String[] args) {
        stackedWordsReplaceDemo_1();
        stackedWordsReplaceDemo_2();
    }

    public static void stackedWordsReplaceDemo_1() {
        // 将下面的字符串转换成"我要学习编程。"
        String str = "我我我我我要要学学学编编编编程程程程。";
        System.out.println(str);
    }

    public static void stackedWordsReplaceDemo_2() {
        String str = "我我..我我...我..要要.学...学学编..编编...编程程...程程。";
        //将所有"."替换成""
        str=str.replaceAll("\\.+","");
        //(.)表示任意一个字符,括号表示设置成一个组,\\1表示根据分组的重复字符,+表示0次或者多次
        //后面的"$1"表示重复字符中只取一个字符
        str=str.replaceAll("(.)\\1+","$1");
        System.out.println(str);
    }
}

###4.使用正则表达式对字符串进行切割

使用正则表达式对字符串进行切割有两种方式:
1)使用String对象的split方法
2)使用Pattern对象的split方法
切割的特点:
1)可以将正则表达式匹配到的字符(串)作为分隔符来对字符串进行切割
2)切割结果为子串组成的字符串数组

class SplitTest {
    public static void main(String[] args) {
        splitDemo();
    }

    public static void splitDemo() { 
        String str = "北.京.欢.迎.您"; 
        String regex = "\\."; 
        String[] strings = str.split(regex); 
        for (String s : strings) { 
            System.out.println(s); 
        } 
    } 
}

##四、综合案例演示


###1.对IP地址进行排序


class IPSort {
    public static void main(String[] args) {
        ipSort();
    }

    /**
     * 将IP地址进行排序
     */
    public static void ipSort() {
        String ip = "127.0.0.1 192.168.0.1 114.114.114.114 8.8.8.8 10.2.33.134 255.255.255.255"; 
        // 1.将IP地址全部替换为xxx.xxx.xxx.xxx样式 
        ip = ip.replaceAll("\\w{1,3}", "00$0"); 
        // 将IP地址每一段都添加00 
        System.out.println(ip); 
        ip = ip.replaceAll("0*(\\w{3})", "$1"); 
        // 去除每一段多余的0,是每一段只保留三位数字 
        System.out.println(ip); 
        // 2.对字符串进行排序 
        String[] arr = ip.split(" "); 
        System.out.println(Arrays.toString(arr)); 
        Arrays.sort(arr); for (String s : arr) { 
            // 去除添加的0 
            s = s.replaceAll("0*(\\w+)", "$1"); 
            System.out.println(s); 
        } 
    } 
}

###2.爬取网页中的电子邮箱实例

class FindEmail {
    public static void main(String[] args) {
        findEmail();
    }

    public static void findEmail() {
        // 简单邮箱匹配规则
        String reg = "\\w+@\\w+(\\.\\w+)+";
        // 1.将正则表达式编译成Pattern对象
        Pattern p = Pattern.compile(reg);
        Matcher m = null;
        try {
            URL url = new URL("https://www.douban.com/group/topic/70867518/");
            URLConnection connection = url.openConnection();
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                // 2.将字符串和Pattern对象进行关联,生成Matcher对象
                if (m == null) {
                    m = p.matcher(line);
                } else {
                    m.reset(line);
                }
                // 3.通过Matcher对象对字符串进行操作
                while (m.find()) {
                    System.out.println(m.group());
                }
            }
        } catch (

                MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值