package testreg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestReg2
{
public static void main(String[] args)
{
String str1 = "";
Object str = "";
//注意:r,n,b等转义字符在java字符串常量中要写成\r,\n,\b等,否则编译都过不去
//s匹配r,n,r和空格
System.out.println("\s匹配\r,\n,\r和空格 "+" tnr".matches("\s{4}"));
//S和s互逆
System.out.println("\S和\s互逆 "+"/".matches("\S"));
//.不匹配r和n
System.out.println(".不匹配\r和\n "+"r".matches("."));
System.out.println("n".matches("."));
//w匹配字母,数字和下划线
System.out.println("\w匹配字母,数字和下划线 "+"a8_".matches("\w\w\w"));
//W和w互逆
System.out.println("\W和\w互逆 "+"&_".matches("\W\w"));
//d匹配数字
System.out.println("\d匹配数字 "+"8".matches("\d"));
//D与d互逆
System.out.println("\D与\d互逆"+"%".matches("\D"));
//两者都匹配但意文不同
System.out.println("======================");
System.out.println("表示\000a匹配\000a "+"n".matches("n"));
System.out.println("表示\n匹配换行 "+"n".matches("\n"));
System.out.println("======================");
//两者都匹配但意文不同
System.out.println("r".matches("r"));
System.out.println("r".matches("\r"));
System.out.println("======================");
//^匹配开头
System.out.println("^匹配开头"+"hell".matches("^hell"));
System.out.println("abcnhell".matches("^hell"));
//$匹配结束
System.out.println("$匹配结束"+"my carnabc".matches(".*ar$"));
System.out.println("my car".matches(".*ar$"));
//b匹配界
System.out.println("\b匹配界 "+"bomb".matches("\bbom."));
System.out.println("bomb".matches(".*mb\b"));
//B与b互逆
System.out.println("\B与\b互逆"+"abc".matches("\Babc"));
//[a-z]匹配a到z的小写字母
System.out.println("[a-z]匹配a到z的小写字母"+"s".matches("[a-z]"));
System.out.println("S".matches("[A-Z]"));
System.out.println("9".matches("[0-9]"));
//取反
System.out.println("取反"+"s".matches("[^a-z]"));
System.out.println("S".matches("[^A-Z]"));
System.out.println("9".matches("[^0-9]"));
//括号的作用
System.out.println("括号的作用"+"aB9".matches("[a-z][A-Z][0-9]"));
System.out.println("aB9bC6".matches("([a-z][A-Z][0-9])+"));
//或运算
System.out.println("或运算"+"two".matches("two|to|2"));
System.out.println("to".matches("two|to|2"));
System.out.println("2".matches("two|to|2"));
//[a-zA-z]==[a-z]|[A-Z]
System.out.println("[a-zA-z]==[a-z]|[A-Z]"+"a".matches("[a-zA-Z]"));
System.out.println("A".matches("[a-zA-Z]"));
System.out.println("a".matches("[a-z]|[A-Z]"));
System.out.println("A".matches("[a-z]|[A-Z]"));
//体会一下以下四个
System.out.println("体会一下以下四个n==========================");
System.out.println(")".matches("[a-zA-Z)]"));
System.out.println(")".matches("[a-zA-Z)_-]"));
System.out.println("_".matches("[a-zA-Z)_-]"));
System.out.println("-".matches("[a-zA-Z)_-]"));
System.out.println("==========================");
System.out.println("b".matches("[abc]"));
//[a-d[f-h]]==[a-df-h]
System.out.println("[a-d[f-h]]==[a-df-h]"+"h".matches("[a-d[f-h]]"));
System.out.println("a".matches("[a-z&&[def]]"));
//取交集
System.out.println("取交集"+"a".matches("[a-z&&[def]]"));
System.out.println("b".matches("[[a-z]&&[e]]"));
//取并
System.out.println("取并"+"9".matches("[[a-c][0-9]]"));
//[a-z&&[^bc]]==[ad-z]
System.out.println("[a-z&&[^bc]]==[ad-z]"+"b".matches("[a-z&&[^bc]]"));
System.out.println("d".matches("[a-z&&[^bc]]"));
//[a-z&&[^m-p]]==[a-lq-z]
System.out.println("[a-z&&[^m-p]]==[a-lq-z]"+"d".matches("[a-z&&[^m-p]]"));
System.out.println("a".matches("\p{Lower}"));
///注意以下体会b的用法(注意,在字符串常量中十目直接写b表退格,所以要写\b
System.out.println("*********************************");
System.out.println("aawordaa".matches(".*\bword\b.*"));
System.out.println("a word a".matches(".*\bword\b.*"));
System.out.println("aawordaa".matches(".*\Bword\B.*"));
System.out.println("a word a".matches(".*\Bword\B.*"));
System.out.println("a word a".matches(".*word.*"));
System.out.println("aawordaa".matches(".*word.*"));
//体会一下组的用法
//组的顺序,只数"("第一个为第一组第二个是第二组……
//第0组表示整个表达式
System.out.println("**************test group**************");
Pattern p = Pattern.compile("(([abc]+)([123]+))([-_%]+)");
Matcher m = p.matcher("aac212-%%");
System.out.println(m.matches());
m = p.matcher("cccc2223%_%_-");
System.out.println(m.matches());
System.out.println("======test group======");
System.out.println(m.group());
System.out.println(m.group(0));
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
System.out.println(m.group(4));
System.out.println(m.groupCount());
System.out.println("========test end()=========");
System.out.println(m.end());
System.out.println(m.end(2));
System.out.println("==========test start()==========");
System.out.println(m.start());
System.out.println(m.start(2));
//test backslash测试反向引用?
Pattern pp1=Pattern.compile("(\d)\1");//这个表达式表示必须有两相同的数字出现
//1表示引用第一个组n表示引用第n个组(必须用\1而不能用1因1在字符串中另有意义(我也知道是什么)
Matcher mm1=pp1.matcher("3345");//33匹配但45不匹配
System.out.println("test backslash测试反向引用");
System.out.println(mm1.find());
System.out.println(mm1.find());
//体会以下不同
System.out.println("==============test find()=========");
System.out.println(m.find());
System.out.println(m.find(2));
System.out.println("这是从第三个字符(index=2)开始找的group结果");
System.out.println(m.group());
System.out.println(m.group(0));
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
m.reset();
System.out.println(m.find());
//测试一个模式可多次匹配一个串
System.out.println("测试一个模式可多次匹配一个串");
Pattern p1 = Pattern.compile("a{2}");
Matcher m1 = p1.matcher("aaaaaa");
//这说明Matcher的matchs()方法是对事个字符串的匹配,
System.out.println(m1.matches());
System.out.println(m1.find());
System.out.println(m1.find());
System.out.println(m1.find());
System.out.println(m1.find());
//再测试matchs()
System.out.println("再测试matchs()");
Pattern p2 = Pattern.compile("(a{2})*");
Matcher m2 = p2.matcher("aaaa");
System.out.println(m2.matches());
System.out.println(m2.matches());
System.out.println(m2.matches());
//所以find是在一个串中找有没有对应的模式,而matchs是完全匹配
//test lookupat()
System.out.println("test lookupat()");
Pattern p3 = Pattern.compile("a{2}");
Matcher m3 = p3.matcher("aaaa");
System.out.println(p3.flags());
System.out.println(m3.lookingAt());
System.out.println(m3.lookingAt());
System.out.println(m3.lookingAt());
//总结以上matchs()是整个匹配且总是从头开始,find是部分匹配且从上一次匹配结束时开始找
//lookingAt也是从头开始,但是部分匹配
System.out.println("======test 空白行========");
System.out.println(" n".matches("^[ \t]*$\n"));
//演示appendXXX的用法
System.out.println("=================test append====================");
Pattern p4 = Pattern.compile("cat");
Matcher m4 = p4.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
boolean result = m4.find();
int i=0;
System.out.println("one cat two cats in the yard");
while(result)
{m4.appendReplacement(sb, "dog");
System.out.println(m4.group());
System.out.println("第"+i+++"次:"+sb.toString());
result = m4.find();
}
System.out.println(sb.toString());
m4.appendTail(sb);
System.out.println(sb.toString());
//test UNIX_LINES
System.out.println("test UNIX_LINES");
Pattern p5=Pattern.compile(".",Pattern.UNIX_LINES);
Matcher m5=p5.matcher("nr");
System.out.println(m5.find());
System.out.println(m5.find());
//test UNIX_LINES
System.out.println("test UNIX_LINES");
Pattern p6=Pattern.compile("(?d).");
Matcher m6=p6.matcher("nr");
System.out.println(m6.find());
System.out.println(m6.find());
//test UNIX_LINES
System.out.println("test UNIX_LINES");
Pattern p7=Pattern.compile(".");
Matcher m7=p7.matcher("nr");
System.out.println(m7.find());
System.out.println(m7.find());
//test CASE_INSENSITIVE
System.out.println("test CASE_INSENSITIVE");
Pattern p8=Pattern.compile("a",Pattern.CASE_INSENSITIVE);
Matcher m8=p8.matcher("aA");
System.out.println(m8.find());
System.out.println(m8.find());
System.out.println("test CASE_INSENSITIVE");
Pattern p9=Pattern.compile("(?i)a");
Matcher m9=p9.matcher("aA");
System.out.println(m9.find());
System.out.println(m9.find());
System.out.println("test CASE_INSENSITIVE");
Pattern p10=Pattern.compile("a");
Matcher m10=p10.matcher("aA");
System.out.println(m10.find());
System.out.println(m10.find());
//test COMMENTS
System.out.println("test COMMENTS");
Pattern p11=Pattern.compile(" a a #ccc",Pattern.COMMENTS);
Matcher m11=p11.matcher("aa a a #ccc");
System.out.println(m11.find());
System.out.println(m11.find());
System.out.println("test COMMENTS");
Pattern p12 = Pattern.compile("(?x) a a #ccc");
Matcher m12 = p12.matcher("aa a a #ccc");
System.out.println(m12.find());
System.out.println(m12.find());
//test MULTILINE这个大家多试试参照我上面对多行模式的理解
System.out.println("test MULTILINE");
Pattern p13=Pattern.compile("^.?",Pattern.MULTILINE|Pattern.DOTALL);
Matcher m13=p13.matcher("helloohelloo,loveroo");
System.out.println(m13.find());
System.out.println("start:"+m13.start()+"end:"+m13.end());
System.out.println(m13.find());
//System.out.println("start:"+m13.start()+"end:"+m13.end());
System.out.println("test MULTILINE");
Pattern p14=Pattern.compile("(?m)^hell.*oo$",Pattern.DOTALL);
Matcher m14=p14.matcher("hello,Worldoonhello,loveroo");
System.out.println(m14.find());
System.out.println("start:"+m14.start()+"end:"+m14.end());
System.out.println(m14.find());
//System.out.println("start:"+m14.start()+"end:"+m14.end());
System.out.println("test MULTILINE");
Pattern p15=Pattern.compile("^hell(.|[^.])*oo$");
Matcher m15=p15.matcher("hello,Worldoonhello,loveroo");
System.out.println(m15.find());
System.out.println("start:"+m15.start()+"end:"+m15.end());
System.out.println(m15.find());
// System.out.println("start:"+m15.start()+"end:"+m15.end());
//test DOTALL
System.out.println("test DOTALL");
Pattern p16=Pattern.compile(".",Pattern.DOTALL);
Matcher m16=p16.matcher("nr");
System.out.println(m16.find());
System.out.println(m16.find());
System.out.println("test DOTALL");
Pattern p17=Pattern.compile(".");
Matcher m17=p17.matcher("nr");
System.out.println(m17.find());
System.out.println(m17.find());
System.out.println("test DOTALL");
Pattern p18=Pattern.compile("(?s).");
Matcher m18=p18.matcher("nr");
System.out.println(m18.find());
System.out.println(m18.find());
//test CANON_EQ这个是jdk的例子但我实在不明白是什么意思,向大家请教
System.out.println("test CANON_EQ");
Pattern p19=Pattern.compile("au030A",Pattern.CANON_EQ);
System.out.println(Character.getType('u030A'));
System.out.println("is"+Character.isISOControl('u030A'));
System.out.println("is"+Character.isUnicodeIdentifierPart('u030A'));
System.out.println(Character.getType('u00E5'));
System.out.println("is"+Character.isISOControl('u00E5'));
Matcher m19=p19.matcher("u00E5");
System.out.println(m19.matches());
System.out.println(Character.getType('u0085'));
System.out.println("is"+Character.isISOControl('u0085'));
//注意下面三个例子体会Greedy,Reluctant and Possessive Quantifiers的不同
Pattern ppp=Pattern.compile(".*foo");
Matcher mmm=ppp.matcher("xfooxxxxxxfoo");
boolean isEnd=false;
int k=0;
System.out.println("==========");
System.out.println("xfooxxxxxxfoo");
while(isEnd==false)
try{
System.out.println("the:"+k++);
System.out.println(mmm.find());
System.out.println(mmm.end());
}catch(Exception e){
isEnd=true;
}
isEnd=false;
Pattern ppp1=Pattern.compile(".*?foo");
Matcher mmm1=ppp1.matcher("xfooxxxxxxfoo");
k=0;
System.out.println("?????????????????????");
System.out.println("xfooxxxxxxfoo");
while(isEnd==false)
try{
System.out.println("the:"+k++);
System.out.println(mmm1.find());
System.out.println(mmm1.end());
}catch(Exception e){
isEnd=true;
}
isEnd=false;
Pattern pp2=Pattern.compile(".*+foo");
Matcher mm2=pp2.matcher("xfooxxxxxxfoo");
int ii=0;
System.out.println("+++++++++++++++++++++++++++");
System.out.println("xfooxxxxxxfoo");
while(isEnd==false)
try{
System.out.println("the:"+ii++);
System.out.println(mm2.find());
System.out.println(mm2.end());
}catch(Exception e){
isEnd=true;
}
}
}



