13-01 Java语言基础(正则表达式)
发布日期:2021-05-06 23:59:41 浏览次数:28 分类:原创文章

本文共 6255 字,大约阅读时间需要 20 分钟。

正则表达式概述


是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。其实就是一种规则,有自己特殊的作用。


案例演示:校验qq号码


要求必须是5-15位数字0不能开头必须都是数字

代码:


public class Demo1_Regex {
public static void main(String[] args) {
System.out.println(checkQQ("012345")); System.out.println(checkQQ("12345")); String regex = "[1-9]\\d{4,14}"; System.out.println("012345".matches(regex)); System.out.println("12345".matches(regex)); } public static boolean checkQQ(String qq) {
boolean flag = true; //如果检测结果不符合要求,flag就设为false if(qq.length() >= 5 && qq.length() <= 15) {
if(!qq.startsWith("0")) {
char[] arr = qq.toCharArray(); for(int i = 0; i < arr.length; i++) {
if(!(arr[i] >= '0' && arr[i] <= '9')) {
flag = false; break; } } }else {
flag = false; } }else {
flag = false; } return flag; }}

输出:


falsetruefalsetrue

正则表达式之字符相关


[abc]:单个字符,范围是a、b和c[^abc]:单个字符,范围是除了a、b、c外的其它所有字符[a-zA-Z]:单个字符,范围是a-z和A-Z(也就是所有大小写英文字母)[a-d[m-p]]:单个字符,范围是a-d和m-p,等同于[a-dm-p](并集)[a-z&&[def]]:单个字符,范围是a-z和def的交集[a-z&&[^bc]]:单个字符,范围是a-z和除了bc外的字符的交集[a-z&&[^m-p]]:单个字符,范围是a-z和除了m-p外的字符的交集

正则表达式之预定义字符


.:代表任意一个字符..:代表任意两个字符\d:代表任意数字,等同于[0-9]\D:代表非数字,等同于[^0-9]\s:空白字符,等同于[ \t\n\x0B\f\r]\S:非空白字符,等同于[^\s]\w:单词字符:[a-zA-Z0-9]\W:非单词字符:[^\w]

代码:


public class Demo4_Regex {
public static void main(String[] args) {
String regex = "."; System.out.println("a".matches(regex)); //true System.out.println("ab".matches(regex)); //false System.out.println("------------------"); String regex1 = ".."; System.out.println("a".matches(regex1)); //false System.out.println("ab".matches(regex1)); //true System.out.println("------------------"); String regex2 = "\\d"; //\是转义字符,\\表示\ System.out.println("2".matches(regex2)); //true System.out.println("a".matches(regex2)); //false System.out.println("------------------"); String regex3 = "\\D"; //非数字字符 System.out.println("2".matches(regex3)); //false System.out.println("a".matches(regex3)); //true System.out.println("------------------"); String regex4 = "\\s"; //空白字符 System.out.println("".matches(regex4)); //空字符 //false System.out.println(" ".matches(regex4)); //一个空格 //true System.out.println(" ".matches(regex4)); //四个空格 //false System.out.println(" ".matches(regex4)); //tab键 //true System.out.println("------------------"); String regex5 = "\\S"; //空白字符 System.out.println("".matches(regex5)); //空 //false System.out.println(" ".matches(regex5)); //一个空格 //false System.out.println(" ".matches(regex5)); //四个空格 //false System.out.println(" ".matches(regex5)); //tab键 //false }}

输出:


truefalse------------------falsetrue------------------truefalse------------------falsetrue------------------falsetruefalsetrue------------------falsefalsefalsefalse

正则表达式之数量词


数量词:


X?  X一次或X一次也没有X*   X零次或X多次X+  X一次或X多次X{n}   X恰好n次X{n,}  X至少n次X{n,m}  X至少n次,但不超过m次

代码:


public class Demo5_Regex {
public static void main(String[] args) {
String regex1 = "[abc]?"; System.out.println("a".matches(regex1)); //true System.out.println("b".matches(regex1)); //true System.out.println("c".matches(regex1)); //true System.out.println("d".matches(regex1)); //false System.out.println("".matches(regex1)); //true System.out.println("-----------------"); String regex2 = "[abc]*"; System.out.println("aaa".matches(regex2)); //true System.out.println("baa".matches(regex2)); //true System.out.println("cdd".matches(regex2)); //false System.out.println("d".matches(regex2)); //false System.out.println("".matches(regex2)); //true System.out.println("-----------------"); String regex3 = "[abc]+"; System.out.println("aaa".matches(regex3)); //true System.out.println("baa".matches(regex3)); //true System.out.println("cdd".matches(regex3)); //false System.out.println("d".matches(regex3)); //false System.out.println("".matches(regex3)); //false }}

输出:


truetruetruefalsetrue-----------------truetruefalsefalsetrue-----------------truetruefalsefalsefalse

正则表达式的分割功能


代码:


public class Demo6_Split {
public static void main(String[] args) {
String s = "金三胖.郭美美"; String[] arr = s.split("\\."); for(String str : arr) {
System.out.println(str); } }}

输出:


金三胖郭美美

分析:


split()分割是按照正则表达式切割的,所以直接写"."的话会把每个字符都当做间隔符,所以字符串数组中啥也没有。如果一定想要用"."来切割,可以加上转义字符。

案例:给指定字符串中的数字排序
代码:


import java.util.Arrays;public class Demo7_Split {
public static void main(String[] args) {
String str = "91 27 46 38 50"; String[] arr = str.split(" "); int[] nums = new int[arr.length]; for(int i = 0; i < arr.length; i++) {
nums[i] = Integer.parseInt(arr[i]); } Arrays.sort(nums); System.out.println(Arrays.toString(nums)); }}

输出:


[27, 38, 46, 50, 91]

正则表达式的替换功能


代码:


public class Demo8_ReplaceAll {
public static void main(String[] args) {
String s = "woaixia2222oxin"; String regex = "\\d"; //\\d代表任意数字 String s2 = s.replaceAll(regex, ""); System.out.println(s2); }}

输出:


woaixiaoxin

正则表达式的分组功能


代码:


public class Demo9_Regex {
public static void main(String[] args) {
// String regex = "(.)\\1(.)\\2"; //\\1表示第一组再出现一次,\\2表示第二组再出现一次// System.out.println("快快乐乐".matches(regex));// System.out.println("快乐快乐".matches(regex)); // // String regex2 = "(..)\\1"; //这两个任意字符再出现一次// System.out.println("高兴高兴".matches(regex2)); String s = "我我...我...我.要...要要要....要学.....学学....学.学.....学..编.编.编...编程...."; String s1 = s.replaceAll("\\.", ""); String s2 = s1.replaceAll("(.)\\1+", "$1"); System.out.println(s1); System.out.println(s2); }}

输出:


我我我我要要要要要学学学学学学编编编编程我要学编程

分析:


s1去为了去除掉小圆点s2去除了重复词而只保留一个。$1的意思是第一组的内容。

Pattern和Matcher的用法


代码:


import java.util.regex.Matcher;import java.util.regex.Pattern;public class Demo10_Pattern {
public static void main(String[] args) {
//把一个字符串中的手机号码获取出来 String s = "我的手机号码是18112312312,曾经用过31231213124,还用过78945612312"; Pattern p = Pattern.compile("\\d{11}"); Matcher m = p.matcher(s); while(m.find()) {
System.out.println(m.group()); } } private static void demo1() {
Pattern p = Pattern.compile("a*b"); //获取到正则表达式 Matcher m = p.matcher("aaaaab"); //获取匹配器 boolean b = m.matches(); //进行匹配 System.out.println(b); }}

输出:


181123123123123121312478945612312
上一篇:13-02 Java语言基础(常用工具类之Math类)
下一篇:12-06 Java语言基础(JDK5新特性自动装箱和拆箱 & Integer面试题)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月26日 15时58分29秒