@Test
public void testpoint(){
//. 匹配除换行符以外的任意字符
System.out.println("a".matches(".")); //true
System.out.println("\n".matches(".")); //\n 回车 false
System.out.println("\t".matches(".")); // \t 相当于tab,缩进 true
System.out.println("\r".matches(".")); //\r 换行 false
System.out.println("abc".matches(".bc"));// true
System.out.println(" ".matches(".")); //true
System.out.println("-----------开朗的分割线--------------");
// \d匹配数字[0-9] digit(数字)单词的首字符,小写表示是,大写表示非
System.out.println("1".matches("\\d")); //true
System.out.println("12".matches("\\d"));//false
System.out.println("12".matches("\\d*"));//true
System.out.println("a".matches("\\d")); //false
System.out.println("-----------忧伤的分割线------------");
// \D匹配非数字[^0-9]
System.out.println("1".matches("\\D")); //false
System.out.println("a".matches("\\D")); //true
System.out.println("-----------活泼的分割线------------");
// \s匹配空白字符[ \t\n\x0B\f\r] space单词的首字符,小写表示是,大写表示非
System.out.println(" ".matches("\\s"));//输出true
System.out.println("\t".matches("\\s"));//输出true
System.out.println("\n".matches("\\s"));//输出true
System.out.println("a".matches("\\s"));//输出false
System.out.println("\f".matches("\\s"));//输出true
System.out.println("-----------可爱的分割线------------");
// \S匹配非空白字符[^\s]
System.out.println(" ".matches("\\S"));//输出false
System.out.println("\t".matches("\\S"));//输出false
System.out.println("\n".matches("\\S"));//输出false
System.out.println("a".matches("\\S"));//输出true
System.out.println("-----------萌萌的分割线------------");
// \w匹配单词字符[a-zA-Z_0-9] word单词的首字符,小写表示是,大写表示非
System.out.println(" ".matches("\\w"));//输出false
System.out.println("a".matches("\\w"));//输出true
System.out.println("1".matches("\\w"));//输出true
System.out.println("A".matches("\\w"));//输出true
System.out.println("\t".matches("\\w"));//输出false
System.out.println("-----------开朗的分割线--------------");
// ^匹配行的开头
//匹配以a开头的字符串
System.out.println("abc".matches("^a.*"));//输出true
System.out.println("abc".matches("^b.*"));//输出false
System.out.println("-----------无知的分割线--------------");
// $匹配行的开头
//匹配以a结尾的字符串
System.out.println("bca".matches(".*a$"));//输出true
System.out.println("bca".matches(".*b$"));//输出false
System.out.println("-----------温柔的分割线--------------");
// *匹配前面的子表达式零次或多次
System.out.println("bca".matches(".*"));//输出true
System.out.println("bca".matches("b.*"));//输出true
System.out.println("bca".matches("d*bca"));//输出true
System.out.println("-----------善良的分割线--------------");
// +匹配前面的子表达式一次或多次
System.out.println("bca".matches(".+ca"));//输出true
System.out.println("bca".matches("b.+"));//输出true
System.out.println("bca".matches("d+bca"));//输出false
System.out.println("-----------暴力的分割线--------------");
// ?匹配前面的子表达式零次或一次
System.out.println("aab".matches("a?aab"));//输出true
System.out.println("aab".matches("a?ab"));//输出true
System.out.println("aab".matches("a?b"));//输出false
System.out.println("-----------冷漠的分割线--------------");
// {n}匹配前面的子表达式确定的 n次,n是一个非负整数。
System.out.println("aab".matches("a{2}b"));//输出true
System.out.println("aab".matches("a{1}ab"));//输出true
System.out.println("aab".matches("a{1}b"));//输出false
System.out.println("-----------热情的分割线--------------");
// {n,}匹配前面的子表达式至少n次,n是一个非负整数。
System.out.println("aab".matches("a{2,}b"));//输出true
System.out.println("aab".matches("a{1,}ab"));//输出true
System.out.println("aab".matches("a{1,}b"));//输出true
System.out.println("aaaaaab".matches("a{1,}b"));//输出true
System.out.println("-----------放荡的分割线--------------");
// {n,m}匹配前面的子表达式至少n次且最多m次,n和m都是非负整数,其中n <= m
System.out.println("aab".matches("a{1,2}b"));//输出true
System.out.println("aab".matches("a{1,2}ab"));//输出true
System.out.println("aaab".matches("a{1,2}b"));//输出false
System.out.println("aaaaaab".matches("a{1,2}b"));//输出false
System.out.println("-----------激情的分割线--------------");
// | 或者,如:x|y匹配x或者y
System.out.println("a".matches("a|b"));//输出true
System.out.println("b".matches("a|b"));//输出true
System.out.println("c".matches("a|b"));//输出false
System.out.println("-----------刺激的分割线--------------");
// []字符集合,匹配所包含的任意一个字符,如[xyz]匹配x或y或z
System.out.println("a".matches("[abc]"));//输出true
System.out.println("b".matches("[abc]"));//输出true
System.out.println("c".matches("[abc]"));//输出true
System.out.println("d".matches("[abc]"));//输出false
System.out.println("-----------木然的分割线--------------");
// [^]非字符集合,匹配未包含的任意字符,如[xyz]匹配不是x并且不是y并且不是z的字符
System.out.println("a".matches("[^abc]"));//输出false
System.out.println("b".matches("[^abc]"));//输出false
System.out.println("c".matches("[^abc]"));//输出false
System.out.println("d".matches("[^abc]"));//输出true
System.out.println("-----------漠然的分割线--------------");
// [a-z]字符范围,匹配指定范围内的任意字符
System.out.println("a".matches("[a-c]"));//输出true
System.out.println("b".matches("[a-c]"));//输出true
System.out.println("c".matches("[a-c]"));//输出true
System.out.println("d".matches("[a-c]"));//输出false
System.out.println("-----------朴实的分割线--------------");
// [^a-z]非字符范围,匹配任何不在指定范围内的任意字符
System.out.println("a".matches("[^a-c]"));//输出false
System.out.println("b".matches("[^a-c]"));//输出false
System.out.println("c".matches("[^a-c]"));//输出false
System.out.println("d".matches("[^a-c]"));//输出true
System.out.println("-----------诚实的分割线--------------");
// \b匹配单词边界,也就是指单词和空格间的位置。 boundary(边界)单词的首字符,小写表示是,大写表示非
System.out.println("name".matches(".*me\\b"));//输出true
System.out.println("mean".matches(".*me\\b"));//输出false
System.out.println("-----------未知的分割线--------------");
// \B匹配非单词边界,也就是指不匹配单词和空格间的位置。
System.out.println("name".matches(".*me\\B.*"));//输出false
System.out.println("mean".matches(".*me\\B.*"));//输出true
}
将正则表达式应用到上面的 URI,各子匹配项包含下面的内容:
public void pattern(){
String str="http://www.runoob.com:8080/html/html-tutorial.html";
String reg="(\\w+)://([^/:]+)(:\\d*)?([^# ]*)"; //result:http://www.runoob.com:8080/html/html-tutorial.html
String reg1="\\w+:"; //http: com:
String reg2="^\\w+:"; //http:
Pattern p=Pattern.compile(reg1);
Matcher m=p.matcher(str);
while(m.find()){
System.out.println(m.group());
}
}
解析:
第一个括号子表达式捕获 Web 地址的协议部分。该子表达式匹配在冒号和两个正斜杠前面的任何单词。
第二个括号子表达式捕获地址的域地址部分。子表达式匹配 / 和 : 之外的一个或多个字符。
第三个括号子表达式捕获端口号(如果指定了的话)。该子表达式匹配冒号后面的零个或多个数字。只能重复一次该子表达式。
最后,第四个括号子表达式捕获 Web 地址指定的路径和 / 或页信息。该子表达式能匹配不包括 # 或空格字符的任何字符序列。
参考博客:http://blog.csdn.net/gnail_oug/article/details/51260106
http://www.runoob.com/regexp/regexp-syntax.html