Java正则表达式 & Java方法
Java正则表达式
正则表达式定义了字符串的模式
正则表达式可以用来搜索、编辑或者处理文本
正则表达式并不仅限于某一种语言,但是每种语言都有细微的差别
Java的正则表达式与Perl最为相似:
java.util.regex包主要包括以下三个类:
- Pattern类:
pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静 态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数
- Matcher类:
Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象
- PatternSyntaxException:
PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。
import java.util.regex.*;
public class RegexTest{
public static void main(String[] args)
{
String content = "I am very beautiful!";
String pattern = ".*very.*";//是否包含very
boolean isMatch = Pattern.matches(pattern,content);
System.out.println("字符串是否包含very?" + isMatch);
}
}
//结果为:字符串是否包含very?true
捕获组
捕获组是把多个字符当一个单独单元进行处理的方法,它通过对括号内的字符分组来创建。
捕获组是通过从左至右计算其开括号来编号。例如,在表达式((A)(B(C)))有四个这样的组:
- ((A)(B(C)))
- (A)
- (B(C))
- (C)
- 通过调用 matcher 对象的 groupCount 方法来查看表达式有多少个分组。groupCount 方法返回一个 int 值,表示matcher对象当前有多个捕获组
- 还有一个特殊的组(group(0)),它总是代表整个表达式。该组不包括在 groupCount 的返回值中
正则表达式语法
- 在其他语言中,\\ 表示:想要在正则表达式中插入一个普通的(字面上的)反斜杠,请不要给它任何特殊的意义。
- 在 Java 中,\\ 表示:要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。
所以,在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。也就是说两个 \\ 代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \\d,而表示一个普通的反斜杠是 \\\\
Matcher 类的方法
序号 | 方法及说明 |
---|---|
1 | public int start() 返回以前匹配的初始索引 |
2 | public int start(int group) 返回在以前的匹配操作期间,由给定组所捕获的子序列的初始索引 |
3 | public int end() 返回最后匹配字符之后的偏移量 |
4 | public int end(int group) 返回在以前的匹配操作期间,由给定组所捕获子序列的最后字符之后的偏移量 |
5 | public boolean lookingAt() 尝试将从区域开头开始的输入序列与该模式匹配 |
6 | public boolean find() 尝试查找与该模式匹配的输入序列的下一个子序列 |
7 | public boolean find(int start) 重置此匹配器,然后尝试查找匹配该模式、从指定索引开始的输入序列的下一个子序列 |
8 | public boolean matches() 尝试将整个区域与模式匹配 |
9 | public Matcher appendReplacement(StringBuffer sb, String replacement) 实现非终端添加和替换步骤 |
10 | public StringBuffer appendTail(StringBuffer sb) 实现终端添加和替换步骤 |
11 | public String replaceAll(String replacement) 替换模式与给定替换字符串相匹配的输入序列的每个子序列 |
12 | public String replaceFirst(String replacement) 替换模式与给定替换字符串匹配的输入序列的第一个子序列 |
13 | public static String quoteReplacement(String s) 返回指定字符串的字面替换字符串。这个方法返回一个字符串,就像传递给Matcher类的appendReplacement 方法一个字面字符串一样工作。 |
PatternSyntaxException 类的方法
- PatternSyntaxException 是一个非强制异常类,它指示一个正则表达式模式中的语法错误
- PatternSyntaxException 类提供了下面的方法
序号 | 方法与说明 |
---|---|
1 | public String getDescription() 获取错误的描述 |
2 | public int getIndex() 获取错误的索引 |
3 | public String getPattern() 获取错误的正则表达式模式 |
4 | public String getMessage() 返回多行字符串,包含语法错误及其索引的描述、错误的正则表达式模式和模式中错误索引的可视化指示 |
举例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
private static final String REGEX = "\\bdog\\b";
private static final String REGEX2 = "cat";
private static final String INPUT = "dogcat cat dogtie cat";
private static final String INPUT2 = "cat dog dog cattie cat";
public static void TestStartEnd (){
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 获取 matcher 对象
Matcher m2 = p.matcher(INPUT2); // 获取 matcher 对象
int count = 0;
int count2=0;
printTest(m,count);
printTest(m2,count2);
Pattern p2 = Pattern.compile(REGEX2);
Matcher m3 = p2.matcher(INPUT); // 获取 matcher 对象
Matcher m4 = p2.matcher(INPUT2); // 获取 matcher 对象
System.out.println("Current REGEX2 is: "+REGEX2);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("Current INPUT2 is: "+INPUT2);
System.out.println("lookingAt(): "+m3.lookingAt());
System.out.println("matches(): "+m3.matches());
System.out.println("lookingAt(): "+m4.lookingAt());
}
private static void printTest(Matcher m,int count)
{
while(m.find()) {
count++;
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
}
}
}