-
Java正则表达式
正则表达式定义了字符串的模式。
正则表达式可以用来搜索、编辑或处理文本。
正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。
Java 正则表达式和 Perl 的是最为相似的。
java.util.regex 包主要包括以下三个类:
- Pattern 类:
pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。
- Matcher 类:
Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。
- PatternSyntaxException:
PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。
以下实例中使用了正则表达式 .*ppy.* 用于查找字符串中是否包了 ppy子串:
String content = "I am a " +
"happy boy";
String pattern = ".*ppy.*";
boolean isMatch = Pattern.matches(pattern,content);
System.out.println("字符串里是否包含了' ppy '字符串?" + isMatch);//字符串里是否包含了' ppy '字符串?true
-
正则表达式语法
在其他语言中,\\ 表示:我想要在正则表达式中插入一个普通的(字面上的)反斜杠,请不要给它任何特殊的意义。
在 Java 中,\\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。
所以,在其他的语言中(如Perl),一个反斜杠 \ 就足以具有转义的作用,而在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。也可以简单的理解在 Java 的正则表达式中,两个 \\ 代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \\d,而表示一个普通的反斜杠是 \\\\。
-
Matcher类的方法
索引方法
索引方法提供了有用的索引值,精确表明输入字符串中在哪能找到匹配:
序号 方法 说明 1 public int start() 返回以前匹配的初始索引 2 public int start(int group) 返回在以前的匹配操作期间,由给定组所捕获的子序列的初始索引 3 public int end() 返回最后匹配字符之后的偏移量 4 public int end(int group) 返回在以前的匹配操作期间,由给定组所捕获子序列的最后字符之后的偏移量
-
研究方法
研究方法用来检查输入字符串并返回一个布尔值,表示是否找到该模式:
序号 方法 说明 1 public boolean lookingAt() 尝试将从区域开头开始的输入序列与该模式匹配 2 public boolean find() 尝试查找与该模式匹配的输入序列的下一个子序列 3 public boolean find(int start) 重置此匹配器,然后尝试查找匹配该模式,从指定索引开始的输入序列的下一个子序列。 4 public boolean matches() 尝试将整个区域与模式匹配。
-
替换方法
替换方法是替换输入字符串里文本的方法:
序号
方法 说明 1 public Matcher appendReplacement(StringBuffer sb,String replacement) 实现非终端添加和替换步骤 2 public StringBuffer appendTail(StringBuffer sb) 实现终端添加和替换步骤 3 public String replaceAll(String relpacement) 替换模式与给定替换字符串相匹配的输入序列的每个子序列 4 public String replaceFirst(String replacement) 替换模式与给定替换字符串匹配的输入序列的第一个子序列 5 public static String quoteReplacement(String s) 返回指定字符串的字面替换字符串,这个方法返回一个字符串,就像传递给Matcher类的appendReplacement方法一个字面字符串一样工作。
-
matches和lookingAt方法
matches 和 lookingAt 方法都用来尝试匹配一个输入序列模式。它们的不同是 matches 要求整个序列都匹配,而lookingAt 不要求。
lookingAt 方法虽然不需要整句都匹配,但是需要从第一个字符开始匹配。
这两个方法经常在输入字符串的开始使用。
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooooooooo";
private static final String INPUT2 = "ooooofoooooooooooo";
//matches和lookingAt方法
pattern1 = Pattern.compile(REGEX1);
matcher1 = pattern1.matcher(INPUT1);
matcher2 = pattern1.matcher(INPUT2);
System.out.println("Current REGEX1 is: "+REGEX1);
System.out.println("Current INPUT1 is: "+INPUT1);
System.out.println("Current INPUT2 is: "+INPUT2);
System.out.println("lookingAt(): "+matcher1.lookingAt()); //true
System.out.println("matches(): "+matcher1.matches()); //false
System.out.println("lookingAt(): "+matcher2.lookingAt());//false
-
replaceFirst和replaceAll方法
replaceFirst 和 replaceAll 方法用来替换匹配正则表达式的文本。不同的是,replaceFirst 替换首次匹配,replaceAll 替换所有匹配。
-
appendReplacement和appendTail方法
private static String REGEX4 = "a*b";
private static String INPUT4 = "aabfooaabfooabfoobkkk";
private static String REPLACE4 = "-";//appendReplacement和appendTail方法
Pattern p4 = Pattern.compile(REGEX4);
Matcher m4 = p4.matcher(INPUT4);
StringBuffer sb4 = new StringBuffer();
while(m4.find()){
m4.appendReplacement(sb4,REPLACE4);
}
m4.appendTail(sb4);
System.out.println(sb4.toString());//-foo-foo-foo-kkk
-
PatternSyntaxException类的方法
PatternSyntaxException 是一个非强制异常类,它指示一个正则表达式模式中的语法错误。
PatternSyntaxException 类提供了下面的方法来帮助我们查看发生了什么错误。
序号 方法 说明 1 public String getDescription() 获取错误的描述 2 public int getIndex() 获取错误的索引 3 public String getPattern() 获取错误的正则表达式模式 4 public String getMessage() 返回多行字符串,包含语法错误及其索引的描述,错误的正则表达式模式和模式中错误索引的可视化指示