1、正则表达式(Regular Expression) 功能是⽤于处理⽂本,包括,查找,替换,删除。
2、在java中,正则表达式是⼀个专门⽤于处理⽂本的对象。
3、正则表达式表现是⼀个字符串,是普通字符和特殊字符共同构成。
4、普通字符包括原义字符和⾮打印字符。
- 原义字符:指字⾯ 的含义保存不变的字符,⽐如 a b c 1 2
- ⾮打印字符: 指转义字符 ⽐如 \n换⾏ \t \r回车
5、特殊字符,即元字符,是指在正则表达式中具有特定含义的字符
6、正则表达式的核⼼对象在java.util.regex包中。
- Pattern 表示⼀个正则表达式,也称为匹配模式。
- Matcher 表⽰正则表达的匹配结果。
7、常⽤的元字符。
-
-
package com.demo01; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo01 { public static void main(String[] args) { // //创建一个正则表达式的模式对象 // Pattern pattern =Pattern.compile("abc"); // //获取模式的匹配对象 // Matcher matcher = pattern.matcher("abc"); // //判断是否匹配成功 // boolean matches =matcher.matches(); // System.out.println(matches?"匹配":"不匹配"); // \w匹配任意字母数字 等价于[a-zA-Z0-9] * 匹配前面表达式任意次 // Pattern pattern1 = Pattern .compile("h\\w*llo"); // Pattern pattern2 = Pattern.compile("h[a-z]*llo"); // Matcher matcher1 = pattern1.matcher("hesgfdgsdfgllo"); // if (matcher1.matches()){ // System.out.println("匹配:"+matcher1.group()); // }else { // System.out.println("不匹配"); // } //普通字符之原义字符 test("1234abcdABCD@","1234abcdABCD@"); //普通字符之转义字符 test("\n","\n"); //特殊字符[xyz]表示字符集合,匹配中括号所包含的任意字符 test("[abc][mno][xyz]","amy"); test("[abc][mno][xyz]","box"); test("[abc][mno][xyz]","cnzz"); //[a-z] test("[a-z][a-z]","aa"); test("[a-z][a-z]","zz"); test("[a-z][a-z]","AA"); //[^xyz]表示匹配非xyz集合中的字符 test("[^a-c][^2-8][^e-m]","f0z"); test("[^a-c][^2-8][^e-m]","fzz"); test("[^a-c][^2-8][^e-m]","c0e"); //x|y表示表达式x或y test("ab[c-g]|uv[h-k]","add"); test("ab[c-g]|uv[h-k]","uvk"); //. 匹配除换行符\n之外的任意单字符 test(".....","abcde"); test(".....","12abc"); test("12.345","12a345"); System.out.println("==============="); //? 匹配前边表达式0或1次 test("ba?","b"); test("a?","a"); test("a?","aa"); test("a?","aaa"); //() 标记表达式的开始和结束 test("(ab)?",""); test("(ab)?","ab"); test("(ab)?","a"); test("(ab)?","b"); test("(ab)?","abc"); //* 匹配前边表达式任意次 test("a*",""); test("a*","a"); test("a*","aa"); test("a*","ab"); System.out.println("============"); //+ 匹配前边表达式1次到多次 test2("a+","abaabaaab"); System.out.println("========="); //{m,n} 最小匹配m次且最多匹配n次(连续) test2("a{3,5}","aa"); test2("a{3,5}","aaa"); test2("a{3,5}","aaaa"); test2("a{3,5}","aaaaa"); test2("a{3,5}","aaaaaa"); System.out.println("-------"); test2("a{3,5}","aaaaabaaacaaaad"); System.out.println("-----------"); //^ 匹配字符串的起始位置 $匹配字符串的结束位置 test("^abc$","abc"); test("^abc$","abc1234"); test("^abc$","aabc"); // \d 匹配数字 等价于[0-9] test("[0-9][0-9][0-9]","123"); test("\\d\\d\\d","123"); test("\\d{3}","123"); // \D 匹配非数字 等价于[^\d] [^0-9] test("[^0-9][^0-9][^0-9]","abc"); test("[^\\d][^\\d][^\\d]","abc"); test("\\D\\D\\D","abc"); test("\\D{3}","abc"); // \w 匹配任意字母和数字 等价于[a-zA-Z0-9] test("[A-Z0-9a-z][A-Z0-9a-z][A-Z0-9a-z]","1aA"); test("[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]","1aA"); test("[0-9A-Za-z][0-9A-Za-z][0-9A-Za-z]","1aA"); test("\\w\\w\\w","1aA"); test("\\w*","1aA"); // \W 不匹配任意字母和数字 等价于[^a-zA-Z0-9] test("[^A-Z0-9a-z][^A-Z0-9a-z][^A-Z0-9a-z]","我和你"); test("[^A-Z0-9a-z][^A-Z0-9a-z][^A-Z0-9a-z]","&*("); test("\\W\\W\\W","&*("); test("\\W{3}","&*("); // \s 匹配空白字符 test("\\s",""); test("\\s"," "); test("\\s","\t"); test("\\s","\n"); // \S 匹配非空白字符 test("\\S","a"); } //局部匹配 public static void test2(String str1,String str2){ Pattern pattern = Pattern.compile(str1); Matcher matcher = pattern.matcher(str2); if (matcher.matches()){ System.out.println("匹配:"+matcher.group()); }else { System.out.println("不匹配"); } } //全局匹配 public static void test(String str1,String str2){ Pattern pattern = Pattern.compile(str1); Matcher matcher = pattern.matcher(str2); if (matcher.matches()){ System.out.println("匹配:"+matcher.group()); }else { System.out.println("不匹配"); } } }