java.util.regex包主要包括以下三个类:
- Pattern类:
pattern对象是一个正则表达式的编译表示。Pattern类没有公共构造方法。要创建一个Pattern对象,你必须首先调用其公共静态编译方法,它返回一个Pattern对象。该方法接受一个正则表达式作为它的第一个参数。
- Matcher类:
Matcher对象是对输入字符串进行解释和匹配操作的引擎。与Pattern类一样,Matcher也没有公共构造方法。你需要调用Pattern对象的matcher方法来获得一个Matcher对象。
- PatternSyntaxException:
PatternSyntaxException是一个非强制异常类,它表示一个正则表达式模式中的语法错误
指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher
对象,依照正则表达式,该对象可以与任意字符序列
匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。
因此,典型的调用顺序是
Pattern p = Pattern.compile
("a*b"); Matcher m = p.matcher
("aaaaab"); boolean b = m.matches
();
在仅使用一次正则表达式时,可以方便地通过此类定义 matches
方法。此方法编译表达式并在单个调用中将输入序列与其匹配。语句
boolean b = Pattern.matches("a*b", "aaaaab");
等效于上面的三个语句,尽管对于重复的匹配而言它效率不高,因为它不允许重用已编译的模式。
此类的实例是不可变的,可供多个并发线程安全使用。Matcher
类的实例用于此目的则不安全。
1 import java.util.regex.Matcher; 2 import java.util.regex.Pattern; 3 4 public class RegexMatches 5 { 6 public static void main( String args[] ){ 7 8 // 按指定模式在字符串查找 9 String line = "This order was placed for QT3000! OK?"; 10 String pattern = "(.*)(\\d+)(.*)"; 11 12 // 创建 Pattern 对象 13 Pattern r = Pattern.compile(pattern); 14 15 // 现在创建 matcher 对象 16 Matcher m = r.matcher(line); 17 if (m.find( )) { 18 System.out.println("Found value: " + m.group(0) ); 19 System.out.println("Found value: " + m.group(1) ); 20 System.out.println("Found value: " + m.group(2) ); 21 } else { 22 System.out.println("NO MATCH"); 23 } 24 } 25 }
常用的:
[...] 位于括号之内的任意字符
[^...] 不在括号之中的任意字符
. 除了换行符之外的任意字符,等价于[^\n]
\w 任何单字字符, 等价于[a-zA-Z0-9]
\W 任何非单字字符,等价于[^a-zA-Z0-9]
\s 任何空白符,等价于[\ t \ n \ r \ f \ v]
\S 任何非空白符,等价于[^\ t \ n \ r \ f \ v]
\d 任何数字,等价于[0-9]
\D 除了数字之外的任何字符,等价于[^0-9]
[\b] 一个退格直接量(特例)
{n, m} 匹配前一项至少n次,但是不能超过m次
{n, } 匹配前一项n次,或者多次
{n} 匹配前一项恰好n次
? 匹配前一项0次或1次,也就是说前一项是可选的. 等价于 {0, 1}
+ 匹配前一项1次或多次,等价于{1,}
* 匹配前一项0次或多次.等价于{0,}
| 选择.匹配的要么是该符号左边的子表达式,要么它右边的子表达式
(...) 分组.将几个项目分为一个单元.这个单元可由 *、+、?和|等符号使用,而且还可以记住和这个组匹配的字符以供此后引用使用
\n 和第n个分组所匹配的字符相匹配.分组是括号中的子表达式(可能是嵌套的).分组号是从左到右计数的左括号数
^ 匹配的是字符的开头,在多行检索中,匹配的是一行的开头
$ 匹配的是字符的结尾,在多行检索中,匹配的是一行的结尾
\b 匹配的是一个词语的边界.简而言之就是位于字符\w 和 \w之间的位置(注意:[\b]匹配的是退格符)
\B 匹配的是非词语的边界的字符
实战:(LeetCode 125. Valid Palindrome)
1 package String; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 /* 7 Given a string, determine if it is a palindrome, 8 considering only alphanumeric characters and ignoring cases. 9 10 For example, 11 "A man, a plan, a canal: Panama" is a palindrome. 12 "race a car" is not a palindrome. 13 14 Note: 15 Have you consider that the string might be empty? This is a good question to ask during an interview. 16 17 For the purpose of this problem, we define empty string as valid palindrome. 18 */ 19 public class isPalindrome125 { 20 public static boolean isPalindrome(String s) { 21 String regEx="[^a-zA-Z0-9]"; 22 Pattern p=Pattern.compile(regEx); 23 Matcher m=p.matcher(s); 24 String str=m.replaceAll("").trim().toLowerCase(); 25 int N=str.length(); 26 for(int i=0;i<N/2;i++){ 27 if(str.charAt(i)!=(str.charAt(N-i-1))) 28 return false; 29 } 30 return true; 31 32 } 33 public static void main(String[] args) { 34 // TODO Auto-generated method stub 35 String s1=""; 36 String s2="a"; 37 String s3=" "; 38 String s4="A man, a plan, a canal: Panama"; 39 String s5="race a car"; 40 String s6="121"; 41 System.out.println(isPalindrome(s1)); 42 System.out.println(isPalindrome(s2)); 43 System.out.println(isPalindrome(s3)); 44 System.out.println(isPalindrome(s4)); 45 System.out.println(isPalindrome(s5)); 46 System.out.println(isPalindrome(s6)); 47 } 48 49 }