• Java正则表达式


    Java正则表达式(Java Regex或Regular Expression)是一种定义用于搜索或操作字符串的模式的API。它广泛用于定义字符串的约束,例如密码和电子邮件验证。学习Java正则表达式教程后,您将能够通过Java Regex测试工具来测试您的正则表达式。Java正则表达式API在java.util.regex包中提供了1个接口和3个类。

    Matcher类和Pattern类提供了Java正则表达式的功能。java.util.regex包提供以下类和接口用于正则表达式:MatchResult接口、Matcher类、Pattern类、PatternSyntaxException类。

    tcher类

    它实现了MatchResult接口。它是一个正则表达式引擎,用于在字符序列上执行匹配操作。

    No.MethodDescription
    1boolean matches()测试正则表达式是否匹配模式。
    2boolean find()查找下一个与模式匹配的表达式。
    3boolean find(int start)从给定的起始编号开始查找与模式匹配的下一个表达式。
    4String group()返回匹配的子序列。
    5int start()返回匹配子序列的起始索引。
    6int end()返回匹配子序列的结束索引。
    7int groupCount()返回匹配子序列的总数。

    Pattern类

    它是正则表达式的编译版本。它用于为正则表达式引擎定义模式。

    No.MethodDescription
    1static Pattern compile(String regex)编译给定的正则表达式并返回模式的实例。
    2Matcher matcher(CharSequence input)创建一个将给定输入与模式相匹配的匹配器。
    3static boolean matches(String regex, CharSequence input)它作为编译和匹配器方法的组合。它编译正则表达式并将给定的输入与模式匹配。
    4String[] split(CharSequence input)围绕给定模式的匹配项拆分给定的输入字符串。
    5String pattern()返回正则表达式模式。

    Java正则表达式示例

    1. import java.util.regex.*;
    2. public class RegexExample1{
    3. public static void main(String args[]){
    4. //第一种方式
    5. Pattern p = Pattern.compile(".s");//代表单个字符
    6. Matcher m = p.matcher("as");
    7. boolean b = m.matches();
    8. //第二种方式
    9. boolean b2=Pattern.compile(".s").matcher("as").matches();
    10. //第三种方式
    11. boolean b3 = Pattern.matches(".s", "as");
    12. System.out.println(b+" "+b2+" "+b3);
    13. }}

    输出

    true true true

    1. import java.util.regex.*;
    2. class RegexExample2{
    3. public static void main(String args[]){
    4. System.out.println(Pattern.matches(".s", "as"));//true (第二个字符是s)
    5. System.out.println(Pattern.matches(".s", "mk"));//false (第二个字符不是 s)
    6. System.out.println(Pattern.matches(".s", "mst"));//false (超过 2 个字符)
    7. System.out.println(Pattern.matches(".s", "amms"));//false (超过 2 个字符)
    8. System.out.println(Pattern.matches("..s", "mas"));//true (第3个字符是s)
    9. }}

    正则表达式字符类

    No.Character ClassDescription
    1[abc]a、b 或 c(简单类)
    2[^abc]除 a、b 或 c 之外的任何字符(否定)
    3[a-zA-Z]a 到 z 或 A 到 Z,包括在内(范围)
    4[a-d[m-p]]a 到 d,或 m 到 p:[a-dm-p](并集)
    5[a-z&&[def]]d、e 或 f(交点)
    6[a-z&&bc]a 到 z,b 和 c 除外:[ad-z](减法)
    7[a-z&&m-p]a 到 z,而不是 m 到 p:[a-lq-z](减法)

    正则表达式字符类示例

    1. import java.util.regex.*;
    2. class RegexExample3{
    3. public static void main(String args[]){
    4. System.out.println(Pattern.matches("[amn]", "abcd"));//false (不是 a 或 m 或 n)
    5. System.out.println(Pattern.matches("[amn]", "a"));//true (在a或m或n中)
    6. System.out.println(Pattern.matches("[amn]", "ammmna"));//false (m 和 a 出现不止一次)
    7. }}

    正则表达式量词

    量词指定字符出现的次数。

    RegexDescription
    X?X出现一次或根本不出现
    X+X出现一次或多次
    X*X 出现零次或多次
    X{n}X只出现n次
    X{n,}X 出现 n 次或更多次
    X{y,z}X 至少出现 y 次但少于 z 次

    正则表达式字符类和量词示例

    1. import java.util.regex.*;
    2. class RegexExample4{
    3. public static void main(String args[]){
    4. System.out.println("? quantifier ....");
    5. System.out.println(Pattern.matches("[amn]?", "a"));//true (a或m或n出现一次)
    6. System.out.println(Pattern.matches("[amn]?", "aaa"));//false (a 出现不止一次)
    7. System.out.println(Pattern.matches("[amn]?", "aammmnn"));//false (m 和 n 来了不止一次)
    8. System.out.println(Pattern.matches("[amn]?", "aazzta"));//false (a 出现不止一次)
    9. System.out.println(Pattern.matches("[amn]?", "am"));//false (a或m或n必须来一次)
    10. System.out.println("+ quantifier ....");
    11. System.out.println(Pattern.matches("[amn]+", "a"));//true (a或m或n一次或多次)
    12. System.out.println(Pattern.matches("[amn]+", "aaa"));//true (a 出现不止一次)
    13. System.out.println(Pattern.matches("[amn]+", "aammmnn"));//true (a或m或n出现不止一次)
    14. System.out.println(Pattern.matches("[amn]+", "aazzta"));//false (z 和 t 不匹配模式)
    15. System.out.println("* quantifier ....");
    16. System.out.println(Pattern.matches("[amn]*", "ammmna"));//true (或m或n可能出现零次或多次)
    17. }}

    正则表达式元字符

    正则表达式元字符作为快捷方式。

    RegexDescription
    .任何字符(可能匹配也可能不匹配终止符)
    \d[0-9] 以外的任何数字
    \D任何非数字,0-9 的缩写
    \s任何空白字符,[\t\n\x0B\f\r] 的缩写
    \S任何非空白字符,\s 的缩写
    \w任何单词字符,[a-zA-Z_0-9] 的缩写
    \W任何非单词字符,\w 的缩写
    \b单词边界
    \B非单词边界

    正则表达式元字符示例

    1. import java.util.regex.*;
    2. class RegexExample5{
    3. public static void main(String args[]){
    4. System.out.println("metacharacters d....");\\d means digit
    5. System.out.println(Pattern.matches("\\d", "abc"));//false (非数字)
    6. System.out.println(Pattern.matches("\\d", "1"));//true (数字出现一次)
    7. System.out.println(Pattern.matches("\\d", "4443"));//false (数字但出现不止一次)
    8. System.out.println(Pattern.matches("\\d", "323abc"));//false (数字和字符)
    9. System.out.println("metacharacters D....");\\D means non-digit
    10. System.out.println(Pattern.matches("\\D", "abc"));//false (非数字但出现不止一次)
    11. System.out.println(Pattern.matches("\\D", "1"));//false (数字)
    12. System.out.println(Pattern.matches("\\D", "4443"));//false (数字)
    13. System.out.println(Pattern.matches("\\D", "323abc"));//false (数字和字符)
    14. System.out.println(Pattern.matches("\\D", "m"));//true (非数字且出现一次)
    15. System.out.println("metacharacters D with quantifier....");
    16. System.out.println(Pattern.matches("\\D*", "mak"));//true (非数字,可能出现 0 次或多次)
    17. }}
    1. /*创建一个只接受字母数字字符的正则表达式。
    2. 它的长度只能是六个字符。*/
    3. import java.util.regex.*;
    4. class RegexExample6{
    5. public static void main(String args[]){
    6. System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun32"));//true
    7. System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "kkvarun32"));//false (超过6个字符)
    8. System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "JA2Uk2"));//true
    9. System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun$2"));//false ($不匹配)
    10. }}
    1. /* 创建一个接受 10 位数字字符的正则表达式
    2. 仅以 7、8 或 9 开头。*/
    3. import java.util.regex.*;
    4. class RegexExample7{
    5. public static void main(String args[]){
    6. System.out.println("by character classes and quantifiers ...");
    7. System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));//true
    8. System.out.println(Pattern.matches("[789][0-9]{9}", "9953038949"));//true
    9. System.out.println(Pattern.matches("[789][0-9]{9}", "99530389490"));//false (11个字符)
    10. System.out.println(Pattern.matches("[789][0-9]{9}", "6953038949"));//false (从 6 开始)
    11. System.out.println(Pattern.matches("[789][0-9]{9}", "8853038949"));//true
    12. System.out.println("by metacharacters ...");
    13. System.out.println(Pattern.matches("[789]{1}\\d{9}", "8853038949"));//true
    14. System.out.println(Pattern.matches("[789]{1}\\d{9}", "3853038949"));//false (从 3 开始)
    15. }}

    Java正则表达式查找器示例

    1. import java.util.regex.Pattern;
    2. import java.util.Scanner;
    3. import java.util.regex.Matcher;
    4. public class RegexExample8{
    5. public static void main(String[] args){
    6. Scanner sc=new Scanner(System.in);
    7. while (true) {
    8. System.out.println("Enter regex pattern:");
    9. Pattern pattern = Pattern.compile(sc.nextLine());
    10. System.out.println("Enter text:");
    11. Matcher matcher = pattern.matcher(sc.nextLine());
    12. boolean found = false;
    13. while (matcher.find()) {
    14. System.out.println("I found the text "+matcher.group()+" starting at index "+
    15. matcher.start()+" and ending at index "+matcher.end());
    16. found = true;
    17. }
    18. if(!found){
    19. System.out.println("No match found.");
    20. }
    21. }
    22. }
    23. }

    输出

    Enter regex pattern: javaEnter text: this is java, do you know javaI found the text java starting at index 8 and ending at index 12I found the text java starting at index 26 and ending at index 30
  • 相关阅读:
    单变量微积分重点(1)
    代码随想录算法训练营第四十一天| LeetCode343. 整数拆分、LeetCode96. 不同的二叉搜索树
    私域流量时代来临,电商企业如何布局?
    C51--WiFi模块ESP8266--AT指令
    Mybatis 插入、修改、删除
    使用MinIO Client客户端实现MySQL数据库跨机备份
    python库安装中Microsoft Visual C++ is required解决方法
    Redis_配置文件详解
    [LeetCode]-链表-3
    Handler-源码分析
  • 原文地址:https://blog.csdn.net/xing_jian1/article/details/134491441