1. Java中的正则表达式应用
1、java.util.regex 包主要包括以下三个类:
Pattern 类:
pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。
Matcher 类:
Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。
PatternSyntaxException:
PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。
2. 下面介绍Pattern和Matcher 类中最常用的几个方法
1、Pattern类
Pattern类中有两个最常用的方法:
(1)boolean isMatch = Pattern.matches("regExp", "string");
matches()方法表示正则表达式regExp是否匹配字符串string,匹配返回true,不匹配返回false
注意:String类也有matches()方法,如"abcd".matches(regExp),其实他们俩是等价的,String类matches()方法就是调用的Pattern.matches()方法:
(2)Pattern pattern = Pattern.compile("regExp");
compile()方法表示编译此正则表达式regExp,返回regExp被编译后的pattern
如果只想知道该字符串是否匹配表达式,则直接使用matches()方法最简单
2、Matcher类
Matcher类没有提供什么静态方法,通过调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象,如
Pattern pattern = Pattern.compile("regExp");
Matcher matcher = pattern.matcher("string");
此时我们就得到了一个Matcher对象,通过此对象就可以对字符串进行操作了
Matcher类常用的方法:
(1)索引方法(精确表明输入字符串中在哪能找到匹配)
public int start() 返回以前匹配的初始索引
public int end() 返回最后匹配字符之后的偏移量。
(2)研究方法(用来检查输入字符串并返回一个布尔值,表示是否找到该模式)
public boolean lookingAt() 尝试将从区域开头开始的输入序列与该模式匹配。
public boolean find() 尝试查找与该模式匹配的输入序列的下一个子序列。
public boolean matches() 尝试将整个区域与模式匹配。
(3)替换方法(替换输入字符串里文本的方法)
public String replaceAll(String replacement) 替换模式与给定替换字符串相匹配的输入序列的每个子序列。
public String replaceFirst(String replacement) 替换模式与给定替换字符串匹配的输入序列的第一个子序列。
3. 实例
1、start 和 end 方法
下面是一个对单词 "cat" 出现在输入字符串中出现次数进行计数的例子:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
private static final String REGEX = "\\bcat\\b";
private static final String INPUT =
"cat cat cat cattie cat";
public static void main( String args[] ){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 获取 matcher 对象
int count = 0;
while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
}
}
}
运行结果:
Match number 1
start(): 0
end(): 3
Match number 2
start(): 4
end(): 7
Match number 3
start(): 8
end(): 11
Match number 4
start(): 19
end(): 22
2、matches 和 lookingAt 方法
matches 和 lookingAt 方法都用来尝试匹配一个输入序列模式。它们的不同是 matches 要求整个序列都匹配,而lookingAt 不要求。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooooooooo";
private static final String INPUT2 = "ooooofoooooooooooo";
private static Pattern pattern;
private static Matcher matcher;
private static Matcher matcher2;
public static void main( String args[] ){
pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT);
matcher2 = pattern.matcher(INPUT2);
System.out.println("Current REGEX is: "+REGEX);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("Current INPUT2 is: "+INPUT2);
System.out.println("lookingAt(): "+matcher.lookingAt());
System.out.println("matches(): "+matcher.matches());
System.out.println("lookingAt(): "+matcher2.lookingAt());
}
}
运行结果:
Current REGEX is: foo
Current INPUT is: fooooooooooooooooo
Current INPUT2 is: ooooofoooooooooooo
lookingAt(): true
matches(): false
lookingAt(): false
3、replaceFirst 和 replaceAll 方法
replaceFirst 和 replaceAll 方法用来替换匹配正则表达式的文本。不同的是,replaceFirst 替换首次匹配,replaceAll 替换所有匹配。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. " +
"All dogs say meow.";
private static String REPLACE = "cat";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
运行结果:
The cat says meow. All cats say meow.