Java之正则表达式

2022/2/4 20:14:30

本文主要是介绍Java之正则表达式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

正则表达式:

正则的使用方便于验证应用

package Demo_2_4_正则表达式;

public class Main {
    public static void main(String[] args) {
        String str = "1234";
        if (str.matches("\\d+")){
            int num = Integer.parseInt(str);
            System.out.println(num * 2);
        }
    }
}

1.【数量:单个】字符匹配

  - 任意字符:表示由任意字符组成。

public class Main {
    public static void main(String[] args) {
        String str = "a";   // 要判断的数据
        String regex = "a";      // 正则表达式,只能匹配单个 a ,不能匹配其它字符或多个 a 
        System.out.println(str.matches(regex));
    }
}

  - \\  :匹配一个" \ "。

  - \n :匹配换行 

  - \t  :匹配制表符

2.【数量:单个】字符集匹配

  - [abc] :表示可能是字母a、b、c中的任意一个

public class Main {
    public static void main(String[] args) {
        String str = "b";   // 要判断的数据
        String regex = "[abc]";      // 正则表达式
        System.out.println(str.matches(regex));
    }
}

  - [^abc]:表示不是a、b、c中的任意一个字母

  - [a-zA-Z]:表示由一个任意字母组成,不区分大小写

public class Main {
    public static void main(String[] args) {
        String str = "a";   // 要判断的数据
        String regex = "[a-zA-Z]";      // 正则表达式
        System.out.println(str.matches(regex));
    }
}

  - [0-9]:表示数字0至9之间任意一个数字组成

public class Main {
    public static void main(String[] args) {
        String str = "1";   // 要判断的数据
        String regex = "[0-9]";      // 正则表达式
        System.out.println(str.matches(regex));
    }
}

3.【数量:单个】简化的字符集

  - " . " :表示任意的一个字符

public class Main {
    public static void main(String[] args) {
        String str = "#";   // 要判断的数据
        String regex = ".";      // 正则表达式
        System.out.println(str.matches(regex));
    }
}

  - \d:等价于[0-9]

  - \D:等价于[^0-9]

  - \s:匹配任意一位空格,可能是空格、换行、制表符;

public class Main {
    public static void main(String[] args) {
        String str = "a\n";   // 要判断的数据
        String regex = "\\D\\s";      // 正则表达式
        System.out.println(str.matches(regex));
    }
}

  - \S:匹配任意非空格数据

  - \w:匹配字母、数字、下划线,等价于[a-zA-Z_0-9]

  - \W:匹配非字母、数字、下划线,等价于[^a-zA-Z_0-9]

4.边界匹配:

  - ^ :匹配边界开始

  - $:匹配边界结束

5.数量表达:默认情况下只有加上了数量单位才可以匹配多位字符

  - " 表达式? " :表示该正则表达式可以出现0次或1次

  - " 表达式* " :表示该正则表达式可以出现0次或多次

  - " 表达式+ " :表示该正则表达式可以出现1次或多次

public class Main {
    public static void main(String[] args) {
        String str = "aabc";   // 要判断的数据
        String regex = "\\w+";      // 正则表达式
        System.out.println(str.matches(regex));
    }
}

  - 表达式{n} :正则表达式的长度正好为n次

  - 表达式{n,} :正则表达式的长度为n次以上

  - 表达式{n.m}:正则表达式的长度在n~ m次

package Demo_2_4_正则表达式;

public class Main {
    public static void main(String[] args) {
        String str = "aabc";   // 要判断的数据
        String regex = "\\w{3,}";      // 正则表达式,\\w表示\w:匹配非字母、数字、下划线,等价于[^a-zA-Z_0-9],匹配3次或以上
        System.out.println(str.matches(regex));
    }
}

6. 逻辑表达式:可以连接多个正则

  - 表达式X表达式Y:X表达式之后紧跟上Y表达式

public class Main {
    public static void main(String[] args) {
        String str = "aabc";   // 要判断的数据
        String regex = "ax";      // 正则表达式,a后面必须跟上x
        System.out.println(str.matches(regex));
    }
}

  - 表达式X | 表达式Y:有一个表达式满足即可

  - (表达式):为表达式一个整体描述,可以为整体描述设置数量单位

 

使用正则匹配:

去掉一串字符串中非字母数字符号:

public class Main {
    public static void main(String[] args) {
        String str = "ax12@#qwetquyw12@$#!()qpdoihj2oi23*#(*!498yqHR98@!#¥(*98HR#*(hr3F*(hF#Q$*(HFH''";   // 要判断的数据
        String regex = "[^a-zA-Z0-9]+";      // 正则表达式
        System.out.println(str.replaceAll(regex,""));
    }
}

将字符串按照字母分割:

public class Main {
    public static void main(String[] args) {
        String str = "a1111b2222c333d4444e7777";   // 要判断的数据
        String regex = "\\d+";      // 正则表达式
        System.out.println(str.replaceAll(regex,""));
        for (String s : str.split(regex)) {
            System.out.println(s);
        }
    }
}

判断是否为小数:

public class Main {
    public static void main(String[] args) {
        String str = "100.223";   // 要判断的数据
        String regex = "\\d+\\.\\d+";      // 正则表达式
        System.out.println(str.matches(regex));
    }
}

但是这种方式存在缺陷,当判断为整数或只有小数点的时候,结果为false,所以进行改进为以下代码:

 

public class Main {
    public static void main(String[] args) {
        String str = "100";   // 要判断的数据
        String regex = "\\d+(\\.\\d+)?";      // 正则表达式
        System.out.println(str.matches(regex));
    }
}

 

判断日期型字符串:

import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) throws Exception{
        String str = "2020-12-11";   // 要判断的数据
        String regex = "\\d{4}-\\d{2}-\\d{2}";      // 正则表达式
        if (str.matches(regex)){    // 符合正则表达式
            System.out.println(new SimpleDateFormat("yyy-MM-dd").parse(str));
        }
    }
}

以上代码只能按照数字格式进行判断,无法进行内容的判断。

判断给定的电话号码是否正确?
电话号码:51283346、\\d{7,8}; 
电话号码:01051283346、(\\d{3,4})?\\d{7,8}; 
电话号码:(010)-51283346、((\\d{3,4})(\\(\\d{3,4}\\)-))?\\d{7,8}。

匹配邮箱:

package Demo_2_4_正则表达式;

import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) throws Exception{
        String str = "cdulmjava@cdu.edu.cn";   // 要判断的数据
        String regex = "[a-zA-Z0-9]*.[a-zA-Z]*.[a-zA-Z]*.([a-zA-Z]*)?";      // 正则表达式
        String regex1 = "[a-zA-Z0-9]\\w+@[a-zA-Z]+.(cn|com|com.cn|net|gov)+";      // 正则表达式
        System.out.println(str.matches(regex1));
    }
}

 



这篇关于Java之正则表达式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程