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. | Method | Description |
|---|---|---|
| 1 | boolean matches() | 测试正则表达式是否匹配模式。 |
| 2 | boolean find() | 查找下一个与模式匹配的表达式。 |
| 3 | boolean find(int start) | 从给定的起始编号开始查找与模式匹配的下一个表达式。 |
| 4 | String group() | 返回匹配的子序列。 |
| 5 | int start() | 返回匹配子序列的起始索引。 |
| 6 | int end() | 返回匹配子序列的结束索引。 |
| 7 | int groupCount() | 返回匹配子序列的总数。 |
Pattern类
它是正则表达式的编译版本。它用于为正则表达式引擎定义模式。
| No. | Method | Description |
|---|---|---|
| 1 | static Pattern compile(String regex) | 编译给定的正则表达式并返回模式的实例。 |
| 2 | Matcher matcher(CharSequence input) | 创建一个将给定输入与模式相匹配的匹配器。 |
| 3 | static boolean matches(String regex, CharSequence input) | 它作为编译和匹配器方法的组合。它编译正则表达式并将给定的输入与模式匹配。 |
| 4 | String[] split(CharSequence input) | 围绕给定模式的匹配项拆分给定的输入字符串。 |
| 5 | String pattern() | 返回正则表达式模式。 |
Java正则表达式示例
-
- import java.util.regex.*;
- public class RegexExample1{
- public static void main(String args[]){
- //第一种方式
- Pattern p = Pattern.compile(".s");//代表单个字符
- Matcher m = p.matcher("as");
- boolean b = m.matches();
-
- //第二种方式
- boolean b2=Pattern.compile(".s").matcher("as").matches();
-
- //第三种方式
- boolean b3 = Pattern.matches(".s", "as");
-
- System.out.println(b+" "+b2+" "+b3);
- }}
输出
true true true
- import java.util.regex.*;
- class RegexExample2{
- public static void main(String args[]){
- System.out.println(Pattern.matches(".s", "as"));//true (第二个字符是s)
- System.out.println(Pattern.matches(".s", "mk"));//false (第二个字符不是 s)
- System.out.println(Pattern.matches(".s", "mst"));//false (超过 2 个字符)
- System.out.println(Pattern.matches(".s", "amms"));//false (超过 2 个字符)
- System.out.println(Pattern.matches("..s", "mas"));//true (第3个字符是s)
- }}
正则表达式字符类
| No. | Character Class | Description |
|---|---|---|
| 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](减法) |
正则表达式字符类示例
- import java.util.regex.*;
- class RegexExample3{
- public static void main(String args[]){
- System.out.println(Pattern.matches("[amn]", "abcd"));//false (不是 a 或 m 或 n)
- System.out.println(Pattern.matches("[amn]", "a"));//true (在a或m或n中)
- System.out.println(Pattern.matches("[amn]", "ammmna"));//false (m 和 a 出现不止一次)
- }}
正则表达式量词
量词指定字符出现的次数。
| Regex | Description |
|---|---|
| X? | X出现一次或根本不出现 |
| X+ | X出现一次或多次 |
| X* | X 出现零次或多次 |
| X{n} | X只出现n次 |
| X{n,} | X 出现 n 次或更多次 |
| X{y,z} | X 至少出现 y 次但少于 z 次 |
正则表达式字符类和量词示例
- import java.util.regex.*;
- class RegexExample4{
- public static void main(String args[]){
- System.out.println("? quantifier ....");
- System.out.println(Pattern.matches("[amn]?", "a"));//true (a或m或n出现一次)
- System.out.println(Pattern.matches("[amn]?", "aaa"));//false (a 出现不止一次)
- System.out.println(Pattern.matches("[amn]?", "aammmnn"));//false (m 和 n 来了不止一次)
- System.out.println(Pattern.matches("[amn]?", "aazzta"));//false (a 出现不止一次)
- System.out.println(Pattern.matches("[amn]?", "am"));//false (a或m或n必须来一次)
-
- System.out.println("+ quantifier ....");
- System.out.println(Pattern.matches("[amn]+", "a"));//true (a或m或n一次或多次)
- System.out.println(Pattern.matches("[amn]+", "aaa"));//true (a 出现不止一次)
- System.out.println(Pattern.matches("[amn]+", "aammmnn"));//true (a或m或n出现不止一次)
- System.out.println(Pattern.matches("[amn]+", "aazzta"));//false (z 和 t 不匹配模式)
-
- System.out.println("* quantifier ....");
- System.out.println(Pattern.matches("[amn]*", "ammmna"));//true (或m或n可能出现零次或多次)
-
- }}
正则表达式元字符
正则表达式元字符作为快捷方式。
| Regex | Description |
|---|---|
| . | 任何字符(可能匹配也可能不匹配终止符) |
| \d | [0-9] 以外的任何数字 |
| \D | 任何非数字,0-9 的缩写 |
| \s | 任何空白字符,[\t\n\x0B\f\r] 的缩写 |
| \S | 任何非空白字符,\s 的缩写 |
| \w | 任何单词字符,[a-zA-Z_0-9] 的缩写 |
| \W | 任何非单词字符,\w 的缩写 |
| \b | 单词边界 |
| \B | 非单词边界 |
正则表达式元字符示例
- import java.util.regex.*;
- class RegexExample5{
- public static void main(String args[]){
- System.out.println("metacharacters d....");\\d means digit
-
- System.out.println(Pattern.matches("\\d", "abc"));//false (非数字)
- System.out.println(Pattern.matches("\\d", "1"));//true (数字出现一次)
- System.out.println(Pattern.matches("\\d", "4443"));//false (数字但出现不止一次)
- System.out.println(Pattern.matches("\\d", "323abc"));//false (数字和字符)
-
- System.out.println("metacharacters D....");\\D means non-digit
-
- System.out.println(Pattern.matches("\\D", "abc"));//false (非数字但出现不止一次)
- System.out.println(Pattern.matches("\\D", "1"));//false (数字)
- System.out.println(Pattern.matches("\\D", "4443"));//false (数字)
- System.out.println(Pattern.matches("\\D", "323abc"));//false (数字和字符)
- System.out.println(Pattern.matches("\\D", "m"));//true (非数字且出现一次)
-
- System.out.println("metacharacters D with quantifier....");
- System.out.println(Pattern.matches("\\D*", "mak"));//true (非数字,可能出现 0 次或多次)
-
- }}
- /*创建一个只接受字母数字字符的正则表达式。
- 它的长度只能是六个字符。*/
-
- import java.util.regex.*;
- class RegexExample6{
- public static void main(String args[]){
- System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun32"));//true
- System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "kkvarun32"));//false (超过6个字符)
- System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "JA2Uk2"));//true
- System.out.println(Pattern.matches("[a-zA-Z0-9]{6}", "arun$2"));//false ($不匹配)
- }}
- /* 创建一个接受 10 位数字字符的正则表达式
- 仅以 7、8 或 9 开头。*/
-
- import java.util.regex.*;
- class RegexExample7{
- public static void main(String args[]){
- System.out.println("by character classes and quantifiers ...");
- System.out.println(Pattern.matches("[789]{1}[0-9]{9}", "9953038949"));//true
- System.out.println(Pattern.matches("[789][0-9]{9}", "9953038949"));//true
-
- System.out.println(Pattern.matches("[789][0-9]{9}", "99530389490"));//false (11个字符)
- System.out.println(Pattern.matches("[789][0-9]{9}", "6953038949"));//false (从 6 开始)
- System.out.println(Pattern.matches("[789][0-9]{9}", "8853038949"));//true
-
- System.out.println("by metacharacters ...");
- System.out.println(Pattern.matches("[789]{1}\\d{9}", "8853038949"));//true
- System.out.println(Pattern.matches("[789]{1}\\d{9}", "3853038949"));//false (从 3 开始)
-
- }}
Java正则表达式查找器示例
-
- import java.util.regex.Pattern;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- public class RegexExample8{
- public static void main(String[] args){
- Scanner sc=new Scanner(System.in);
- while (true) {
- System.out.println("Enter regex pattern:");
- Pattern pattern = Pattern.compile(sc.nextLine());
- System.out.println("Enter text:");
- Matcher matcher = pattern.matcher(sc.nextLine());
- boolean found = false;
- while (matcher.find()) {
- System.out.println("I found the text "+matcher.group()+" starting at index "+
- matcher.start()+" and ending at index "+matcher.end());
- found = true;
- }
- if(!found){
- System.out.println("No match found.");
- }
- }
- }
- }
输出
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