Java正则表达式
Java 正则表达式和 Perl 的是最为相似的。
java.util.regex 包主要包括以下一个接口三个类:
Pattern 类:
pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。
Matcher 类:
Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。
PatternSyntaxException:
PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。
MatchResult接口
所有已知实现类:
Matcher
版本开始:
JDK 1.5
public interface MatchResult
匹配操作的结果。
此接口包含用于确定与正则表达式匹配的结果的查询方法。比赛的界限,组和组的边界可以看出,但通过MatchResult不改。
Id | Modifier and Type | Method | Description |
---|---|---|---|
1 | int | end() | 返回最后一个字符匹配的偏移量。 |
2 | int | end(int group) | 返回偏移后序列捕获的组在本场比赛的最后一个字符。 |
3 | String | group() | 返回由以前的匹配输入序列。 |
4 | String | group(int group) | 返回被给定组以前的匹配操作在输入序列。 |
5 | int | groupCount() | 返回在这个匹配结果的模式中捕获组的数量。 |
6 | int | start() | 返回匹配的开始索引。 |
7 | int | start(int group) | 返回序列捕获的组在这场比赛开始指数。 |
正则表达式的高级应用
详细查看JDK1.8的API,java.util.regex包下pattren类
package com.bennett.test1007;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest2 {
public static void main(String[] args) {
// test1();
// test2();
// test3();
// test4();
// test5();
test6();
// test7();
}
private static void test7() {
// 查看java.lang包下的string类
// split结果阈值limit
String input="论语,孟子,大学,中庸,,";
String[] array=input.split("[,,]",0);//
System.out.println(array.length==4);
array=input.split("[,,]",-1);//将每一个匹配到的分割符作为分割点
System.out.println(array.length==6);
array=input.split("[,,]",2);
System.out.println(array.length);//1 [论语, //2 孟子,大学,中庸,,]
System.out.println(Arrays.toString(array));
}
private static void test6() {
// 字符的捕获
Pattern pattern=Pattern.compile("<b>(\\S+?)</b>",Pattern.MULTILINE);//模板
String input="aaa<b>如梦令</b><b>满江红</b>vvv";
Matcher matcher = pattern.matcher(input);//匹配器
// System.out.println(matcher.find());
while(matcher.find()) {
System.out.println(matcher.start());//匹配字符的开始下标
System.out.println(matcher.end());//匹配字符的结束下标
System.out.println(matcher.group());//分组捕获
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
// System.out.println(matcher.group(2));
}
}
private static void test5() {
//反向引用
String regex="(\\w{3})\\1";
System.out.println("123456".matches(regex)==false);
System.out.println("123123".matches(regex)==true);
System.out.println("456456".matches(regex)==true);
System.out.println("abcabc".matches(regex)==true);
regex="(\\w{2})\\1+";
System.out.println("12121212".matches(regex)==true);
System.out.println("abab".matches(regex)==true);
}
private static void test4() {
// 匹配模式
// 最大匹配(Greedy ,贪婪模式)
// String regex="(<b>(\\S+)</b>)";
// 最小匹配 (Reluctant ,勉强模式)
// String regex="(<b>(\\S+?)</b>)";
// 独占模式(Possessive )
// System.out.println("<b>如梦令</b><b>满江红</b>".replaceAll(regex, "$2"));
String regex="\\S+b";
System.out.println("aaaab".matches(regex));
regex="\\S++b";//独占模式不会回溯
System.out.println("aaaab".matches(regex)==false);
}
private static void test3() {
// String regex="\\$";
String regex="\\p{Sc}";
System.out.println("$".matches(regex));
System.out.println("¥".matches(regex));
System.out.println("".matches(regex));
System.out.println("a".matches(regex)==false);
regex="\\p{InGreek}";
System.out.println("a".matches(regex)==false);
System.out.println("α".matches(regex));
System.out.println("Π".matches(regex));
}
private static void test2() {
// java.lang.Character classes
String regex="\\p{javaLowerCase}";
System.out.println("a".matches(regex)==true);
regex="\\p{javaMirrored}";
System.out.println("(".matches(regex)==true);
System.out.println("<".matches(regex)==true);
System.out.println("《".matches(regex)==true);
System.out.println(",".matches(regex)==false);
}
private static void test1() {
String regex="\\p{Lower}";
System.out.println("a".matches(regex)==true);
System.out.println("A".matches(regex)==false);
regex = "\\p{Alpha}";//英文字母大小写皆可
System.out.println("a".matches(regex)==true);
System.out.println("A".matches(regex)==true);
regex = "\\p{Blank}";
System.out.println(" ".matches(regex)==true);
System.out.println("\t".matches(regex)==true);
System.out.println("a".matches(regex)==false);
regex = "\\p{Cntrl}";
System.out.println("a".matches(regex)==true);
}
}