正则表达式

        正则表达式是一种强大的字符串匹配工具,用于在文本中查找、替换和提取特定模式的字符串。它由一系列字符和特殊字符组成,可以表示字符串的模式。使用正则表达式可以方便地进行字符串的匹配、验证和处理。

        正则表达式的模式由普通字符和特殊字符组成。普通字符表示自身,特殊字符用来表示一类字符或一段模式。

正则表达式

说明

\d

匹配任何一个字母字符。示例:"a", "Z"

\D

匹配任何一个非数字字符。示例:"abc", "_"

\w

匹配任何一个字母字符。示例:"a", "Z"

\W

匹配任何一个非字母字符。示例:"1", "%"

\s

匹配任何一个空白字符,包括空格、制表符、换行符等" ", "\t", "\n"

\S

匹配任何一个非空白字符。

.

匹配任何一个字符,包括字母、数字、特殊字符等。"a", "1", "%"

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

public class RegexExample {
    public static void main(String[] args) {
        String text = "Hello, 123 world!";
        
        // 匹配数字字符集合
        Pattern pattern1 = Pattern.compile("\\d");
        Matcher matcher1 = pattern1.matcher(text);
        while (matcher1.find()) {
            System.out.println("匹配到数字字符: " + matcher1.group());
        }
        
        // 匹配非数字字符集合
        Pattern pattern2 = Pattern.compile("\\D");
        Matcher matcher2 = pattern2.matcher(text);
        while (matcher2.find()) {
            System.out.println("匹配到非数字字符: " + matcher2.group());
        }
        
        // 匹配字母字符集合
        Pattern pattern3 = Pattern.compile("\\w");
        Matcher matcher3 = pattern3.matcher(text);
        while (matcher3.find()) {
            System.out.println("匹配到字母字符: " + matcher3.group());
        }
        
        // 匹配非字母字符集合
        Pattern pattern4 = Pattern.compile("\\W");
        Matcher matcher4 = pattern4.matcher(text);
        while (matcher4.find()) {
            System.out.println("匹配到非字母字符: " + matcher4.group());
        }
        
        // 匹配空白字符集合
        Pattern pattern5 = Pattern.compile("\\s");
        Matcher matcher5 = pattern5.matcher(text);
        while (matcher5.find()) {
            System.out.println("匹配到空白字符: " + matcher5.group());
        }
        
        // 匹配非空白字符集合
        Pattern pattern6 = Pattern.compile("\\S");
        Matcher matcher6 = pattern6.matcher(text);
        while (matcher6.find()) {
            System.out.println("匹配到非空白字符: " + matcher6.group());
        }
        
        // 匹配任意字符集合
        Pattern pattern7 = Pattern.compile(".");
        Matcher matcher7 = pattern7.matcher(text);
        while (matcher7.find()) {
            System.out.println("匹配到任意字符: " + matcher7.group());
        }
    }
}

 

正则表达式

说明

[abc]

匹配单个字符,它可以是"a""b""c"中的任意一个

[a-z]

匹配单个小写字母,它可以是字母表中的任何一个小写字母。

[abcABC]

匹配单个字符,它可以是"a""b""c""A""B""C"中的任意一个。

[a-zA-Z0-9]

匹配单个字符,它可以是字母(小写或大写)或数字中的任何一个。

[a-z&&[^bc]]

匹配单个字符,它是一个小写字母,但不是"b""c"&&表示交集的关系,即匹配的字符必须同时满足两个条件,即是小写字母,但不是"b""c"

[^abc]

匹配任何一个非abc的任意字符。

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

public class RegexExample {
    public static void main(String[] args) {
        String text = "abc123ABCxyz";

        // [abc]
        Pattern pattern1 = Pattern.compile("[abc]");
        Matcher matcher1 = pattern1.matcher(text);
        while (matcher1.find()) {
            System.out.println("匹配到字符 [abc]: " + matcher1.group());
        }

        // [a-z]
        Pattern pattern2 = Pattern.compile("[a-z]");
        Matcher matcher2 = pattern2.matcher(text);
        while (matcher2.find()) {
            System.out.println("匹配到小写字母 [a-z]: " + matcher2.group());
        }

        // [abcABC]
        Pattern pattern3 = Pattern.compile("[abcABC]");
        Matcher matcher3 = pattern3.matcher(text);
        while (matcher3.find()) {
            System.out.println("匹配到字符 [abcABC]: " + matcher3.group());
        }

        // [a-zA-Z0-9]
        Pattern pattern4 = Pattern.compile("[a-zA-Z0-9]");
        Matcher matcher4 = pattern4.matcher(text);
        while (matcher4.find()) {
            System.out.println("匹配到字母或数字 [a-zA-Z0-9]: " + matcher4.group());
        }

        // [a-z&&[^bc]]
        Pattern pattern5 = Pattern.compile("[a-z&&[^bc]]");
        Matcher matcher5 = pattern5.matcher(text);
        while (matcher5.find()) {
            System.out.println("匹配到非b和c的小写字母 [a-z&&[^bc]]: " + matcher5.group());
        }

        // [^abc]
        Pattern pattern6 = Pattern.compile("[^abc]");
        Matcher matcher6 = pattern6.matcher(text);
        while (matcher6.find()) {
            System.out.println("匹配到非a、b、c的字符 [^abc]: " + matcher6.group());
        }
    }
}

正则表达式

说明

X?

表示模式X出现0次或1次。问号?表示前面的字符或表达式在匹配中是可选的。

X*

表示模式X出现0次或多次。星号*表示前面的字符或表达式可以重复任意次数,包括0次。

X+

表示模式X出现1次或多次。加号+表示前面的字符或表达式至少出现一次。

X{n}

表示模式X出现恰好n次。其中,n是一个非负整数。

X{n,}

表示模式X出现至少n次。其中,n是一个非负整数,而逗号后面为空表示没有上限。

X{n,m}

表示模式X出现至少n次,但不超过m次。其中,nm都是非负整数,并且n小于等于m

 

import re

text = "XX XY XXX XXXY XXXX"

# X?
pattern1 = re.compile("X?")
matches1 = pattern1.findall(text)
print("X? 匹配结果:", matches1)

# X*
pattern2 = re.compile("X*")
matches2 = pattern2.findall(text)
print("X* 匹配结果:", matches2)

# X+
pattern3 = re.compile("X+")
matches3 = pattern3.findall(text)
print("X+ 匹配结果:", matches3)

# X{n}
pattern4 = re.compile("X{2}")
matches4 = pattern4.findall(text)
print("X{2} 匹配结果:", matches4)

# X{n,}
pattern5 = re.compile("X{2,}")
matches5 = pattern5.findall(text)
print("X{2,} 匹配结果:", matches5)

# X{n,m}
pattern6 = re.compile("X{2,3}")
matches6 = pattern6.findall(text)
print("X{2,3} 匹配结果:", matches6)

综合练习:

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

public class RegexExercise {
    public static void main(String[] args) {
        // 要匹配的文本
        String text = "这是一个示例文本,其中包含日期2024-03-25、电话号码(123)456-7890和邮箱example@example.com。";

        // 匹配日期
        Pattern datePattern = Pattern.compile("\\b\\d{4}-\\d{2}-\\d{2}\\b");
        Matcher dateMatcher = datePattern.matcher(text);
        while (dateMatcher.find()) {
            System.out.println("匹配到日期: " + dateMatcher.group());
        }

        // 匹配电话号码
        Pattern phonePattern = Pattern.compile("\\(\\d{3}\\)\\d{3}-\\d{4}");
        Matcher phoneMatcher = phonePattern.matcher(text);
        while (phoneMatcher.find()) {
            System.out.println("匹配到电话号码: " + phoneMatcher.group());
        }

        // 匹配邮箱地址
        Pattern emailPattern = Pattern.compile("\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b");
        Matcher emailMatcher = emailPattern.matcher(text);
        while (emailMatcher.find()) {
            System.out.println("匹配到邮箱地址: " + emailMatcher.group());
        }
    }
}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值