正则表达式教程:从入门到精通

大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!

一、引言

正则表达式是一种强大的字符串匹配工具,广泛应用于文本搜索、数据验证和文本处理等领域。本文将从基础概念开始,逐步介绍正则表达式的高级用法,并通过Java代码示例帮助大家掌握正则表达式的使用。

二、基础概念

正则表达式由一系列字符和特殊符号组成,用于描述字符串模式。以下是一些基础概念和符号:

  1. 字符:普通字符直接匹配自身,例如a匹配字符a
  2. 元字符:特殊字符具有特定含义,如.匹配任意单个字符,*表示前面的字符出现零次或多次。
  3. 字符集:方括号[]定义一个字符集,如[abc]匹配abc中的任意一个字符。
  4. 预定义字符集:如\d匹配任意数字,\w匹配任意字母或数字,\s匹配任意空白字符。

三、基本用法

  1. 匹配单个字符
package cn.juwatech.regex;

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

public class SingleCharacterMatch {
    public static void main(String[] args) {
        String input = "abc";
        String regex = "a";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        if (matcher.find()) {
            System.out.println("Match found: " + matcher.group());
        } else {
            System.out.println("No match found");
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  1. 匹配任意字符
package cn.juwatech.regex;

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

public class AnyCharacterMatch {
    public static void main(String[] args) {
        String input = "abc";
        String regex = ".";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        while (matcher.find()) {
            System.out.println("Match found: " + matcher.group());
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  1. 使用字符集
package cn.juwatech.regex;

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

public class CharacterSetMatch {
    public static void main(String[] args) {
        String input = "a1b2c3";
        String regex = "[abc]";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        while (matcher.find()) {
            System.out.println("Match found: " + matcher.group());
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

四、高级用法

  1. 量词

量词用于指定字符出现的次数。常用量词包括:

  • *:零次或多次
  • +:一次或多次
  • ?:零次或一次
  • {n}:恰好n次
  • {n,}:至少n次
  • {n,m}:至少n次,至多m次
package cn.juwatech.regex;

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

public class QuantifierMatch {
    public static void main(String[] args) {
        String input = "aaaab";
        String regex = "a{3,}";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        if (matcher.find()) {
            System.out.println("Match found: " + matcher.group());
        } else {
            System.out.println("No match found");
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  1. 分组

分组通过括号()实现,可以捕获匹配的子字符串。

package cn.juwatech.regex;

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

public class GroupMatch {
    public static void main(String[] args) {
        String input = "2023-08-01";
        String regex = "(\\d{4})-(\\d{2})-(\\d{2})";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        if (matcher.find()) {
            System.out.println("Match found: " + matcher.group(0));
            System.out.println("Year: " + matcher.group(1));
            System.out.println("Month: " + matcher.group(2));
            System.out.println("Day: " + matcher.group(3));
        } else {
            System.out.println("No match found");
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  1. 反向引用

反向引用用于匹配前面捕获的分组。

package cn.juwatech.regex;

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

public class BackreferenceMatch {
    public static void main(String[] args) {
        String input = "abab";
        String regex = "(\\w)\\1";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        while (matcher.find()) {
            System.out.println("Match found: " + matcher.group());
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  1. 零宽断言

零宽断言包括正向先行断言(?=)、负向先行断言(?!)、正向后行断言(?<=)和负向后行断言(?<!

package cn.juwatech.regex;

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

public class LookaheadMatch {
    public static void main(String[] args) {
        String input = "foo123bar";
        String regex = "\\d+(?=bar)";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        if (matcher.find()) {
            System.out.println("Match found: " + matcher.group());
        } else {
            System.out.println("No match found");
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

五、实际应用

  1. 邮箱验证
package cn.juwatech.regex;

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

public class EmailValidation {
    public static void main(String[] args) {
        String input = "test@example.com";
        String regex = "^[\\w.-]+@[\\w.-]+\\.\\w{2,}$";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        if (matcher.matches()) {
            System.out.println("Valid email address");
        } else {
            System.out.println("Invalid email address");
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  1. 电话号码提取
package cn.juwatech.regex;

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

public class PhoneNumberExtraction {
    public static void main(String[] args) {
        String input = "Contact us at (123) 456-7890 or 987-654-3210.";
        String regex = "\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        while (matcher.find()) {
            System.out.println("Phone number found: " + matcher.group());
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

六、总结

正则表达式是处理文本的强大工具,通过学习和掌握其基本概念和高级用法,可以有效地解决各种文本处理问题。无论是简单的匹配任务,还是复杂的数据提取和验证,正则表达式都能提供简洁而灵活的解决方案。

本文著作权归聚娃科技微赚淘客系统开发者团队,转载请注明出处!