📄 彻底研究正则表达式 __.htm
字号:
System.out.println(<FONT color=#00ed0>"*********************************"</FONT>);
System.out.println(<FONT color=#00ed0>"aawordaa"</FONT>.matches(<FONT color=#00ed0>".*\\bword\\b.*"</FONT>));
System.out.println(<FONT color=#00ed0>"a word a"</FONT>.matches(<FONT color=#00ed0>".*\\bword\\b.*"</FONT>));
System.out.println(<FONT color=#00ed0>"aawordaa"</FONT>.matches(<FONT color=#00ed0>".*\\Bword\\B.*"</FONT>));
System.out.println(<FONT color=#00ed0>"a word a"</FONT>.matches(<FONT color=#00ed0>".*\\Bword\\B.*"</FONT>));
System.out.println(<FONT color=#00ed0>"a word a"</FONT>.matches(<FONT color=#00ed0>".*word.*"</FONT>));
System.out.println(<FONT color=#00ed0>"aawordaa"</FONT>.matches(<FONT color=#00ed0>".*word.*"</FONT>));
<FONT color=#0d00e0>//体会一下组的用法</FONT>
<FONT color=#0d00e0>//组的顺序,只数"("第一个为第一组第二个是第二组……</FONT>
<FONT color=#0d00e0>//第0组表示整个表达式</FONT>
System.out.println(<FONT color=#00ed0>"**************test group**************"</FONT>);
Pattern p = Pattern.compile(<FONT color=#00ed0>"(([abc]+)([123]+))([-_%]+)"</FONT>);
Matcher m = p.matcher(<FONT color=#00ed0>"aac212-%%"</FONT>);
System.out.println(m.matches());
m = p.matcher(<FONT color=#00ed0>"cccc2223%_%_-"</FONT>);
System.out.println(m.matches());
System.out.println(<FONT color=#00ed0>"======test group======"</FONT>);
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(<FONT color=#00ed0>"========test end()========="</FONT>);
System.out.println(m.end());
System.out.println(m.end(2));
System.out.println(<FONT color=#00ed0>"==========test start()=========="</FONT>);
System.out.println(m.start());
System.out.println(m.start(2));
<FONT color=#0d00e0>//test backslash测试反向引用?</FONT>
Pattern pp1=Pattern.compile(<FONT color=#00ed0>"(\\d)\\1"</FONT>);<FONT color=#0d00e0>//这个表达式表示必须有两相同的数字出现</FONT>
<FONT color=#0d00e0>//\1表示引用第一个组\n表示引用第n个组(必须用\\1而不能用\1因\1在字符串中另有意义(我也知道是什么)</FONT>
Matcher mm1=pp1.matcher(<FONT color=#00ed0>"3345"</FONT>);<FONT color=#0d00e0>//33匹配但45不匹配</FONT>
System.out.println(<FONT color=#00ed0>"test backslash测试反向引用"</FONT>);
System.out.println(mm1.find());
System.out.println(mm1.find());
<FONT color=#0d00e0>//体会以下不同</FONT>
System.out.println(<FONT color=#00ed0>"==============test find()========="</FONT>);
System.out.println(m.find());
System.out.println(m.find(2));
System.out.println(<FONT color=#00ed0>"这是从第三个字符(index=2)开始找的group结果"</FONT>);
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());
<FONT color=#0d00e0>//测试一个模式可多次匹配一个串</FONT>
System.out.println(<FONT color=#00ed0>"测试一个模式可多次匹配一个串"</FONT>);
Pattern p1 = Pattern.compile(<FONT color=#00ed0>"a{2}"</FONT>);
Matcher m1 = p1.matcher(<FONT color=#00ed0>"aaaaaa"</FONT>);
<FONT color=#0d00e0>//这说明Matcher的matchs()方法是对事个字符串的匹配,</FONT>
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());
<FONT color=#0d00e0>//再测试matchs()</FONT>
System.out.println(<FONT color=#00ed0>"再测试matchs()"</FONT>);
Pattern p2 = Pattern.compile(<FONT color=#00ed0>"(a{2})*"</FONT>);
Matcher m2 = p2.matcher(<FONT color=#00ed0>"aaaa"</FONT>);
System.out.println(m2.matches());
System.out.println(m2.matches());
System.out.println(m2.matches());
<FONT color=#0d00e0>//所以find是在一个串中找有没有对应的模式,而matchs是完全匹配</FONT>
<FONT color=#0d00e0>//test lookupat()</FONT>
System.out.println(<FONT color=#00ed0>"test lookupat()"</FONT>);
Pattern p3 = Pattern.compile(<FONT color=#00ed0>"a{2}"</FONT>);
Matcher m3 = p3.matcher(<FONT color=#00ed0>"aaaa"</FONT>);
System.out.println(p3.flags());
System.out.println(m3.lookingAt());
System.out.println(m3.lookingAt());
System.out.println(m3.lookingAt());
<FONT color=#0d00e0>//总结以上matchs()是整个匹配且总是从头开始,find是部分匹配且从上一次匹配结束时开始找</FONT>
<FONT color=#0d00e0>//lookingAt也是从头开始,但是部分匹配</FONT>
System.out.println(<FONT color=#00ed0>"======test 空白行========"</FONT>);
System.out.println(<FONT color=#00ed0>" \n"</FONT>.matches(<FONT color=#00ed0>"^[ \\t]*$\\n"</FONT>));
<FONT color=#0d00e0>//演示appendXXX的用法</FONT>
System.out.println(<FONT color=#00ed0>"=================test append===================="</FONT>);
Pattern p4 = Pattern.compile(<FONT color=#00ed0>"cat"</FONT>);
Matcher m4 = p4.matcher(<FONT color=#00ed0>"one cat two cats in the yard"</FONT>);
StringBuffer sb = <FONT color=#00a000><B>new</B></FONT> StringBuffer();
<FONT color=#00a000><B>boolean</B></FONT> result = m4.find();
<FONT color=#00a000><B>int</B></FONT> i=0;
System.out.println(<FONT color=#00ed0>"one cat two cats in the yard"</FONT>);
<FONT color=#00a000><B>while</B></FONT>(result)
<FONT color=#00a000>{</FONT>m4.appendReplacement(sb, <FONT color=#00ed0>"dog"</FONT>);
System.out.println(m4.group());
System.out.println(<FONT color=#00ed0>"第"</FONT>+i+++<FONT color=#00ed0>"次:"</FONT>+sb.toString());
result = m4.find();
<FONT color=#00a000>}</FONT>
System.out.println(sb.toString());
m4.appendTail(sb);
System.out.println(sb.toString());
<FONT color=#0d00e0>//test UNIX_LINES</FONT>
System.out.println(<FONT color=#00ed0>"test UNIX_LINES"</FONT>);
Pattern p5=Pattern.compile(<FONT color=#00ed0>"."</FONT>,Pattern.UNIX_LINES);
Matcher m5=p5.matcher(<FONT color=#00ed0>"\n\r"</FONT>);
System.out.println(m5.find());
System.out.println(m5.find());
<FONT color=#0d00e0>//test UNIX_LINES</FONT>
System.out.println(<FONT color=#00ed0>"test UNIX_LINES"</FONT>);
Pattern p6=Pattern.compile(<FONT color=#00ed0>"(?d)."</FONT>);
Matcher m6=p6.matcher(<FONT color=#00ed0>"\n\r"</FONT>);
System.out.println(m6.find());
System.out.println(m6.find());
<FONT color=#0d00e0>//test UNIX_LINES</FONT>
System.out.println(<FONT color=#00ed0>"test UNIX_LINES"</FONT>);
Pattern p7=Pattern.compile(<FONT color=#00ed0>"."</FONT>);
Matcher m7=p7.matcher(<FONT color=#00ed0>"\n\r"</FONT>);
System.out.println(m7.find());
System.out.println(m7.find());
<FONT color=#0d00e0>//test CASE_INSENSITIVE</FONT>
System.out.println(<FONT color=#00ed0>"test CASE_INSENSITIVE"</FONT>);
Pattern p8=Pattern.compile(<FONT color=#00ed0>"a"</FONT>,Pattern.CASE_INSENSITIVE);
Matcher m8=p8.matcher(<FONT color=#00ed0>"aA"</FONT>);
System.out.println(m8.find());
System.out.println(m8.find());
System.out.println(<FONT color=#00ed0>"test CASE_INSENSITIVE"</FONT>);
Pattern p9=Pattern.compile(<FONT color=#00ed0>"(?i)a"</FONT>);
Matcher m9=p9.matcher(<FONT color=#00ed0>"aA"</FONT>);
System.out.println(m9.find());
System.out.println(m9.find());
System.out.println(<FONT color=#00ed0>"test CASE_INSENSITIVE"</FONT>);
Pattern p10=Pattern.compile(<FONT color=#00ed0>"a"</FONT>);
Matcher m10=p10.matcher(<FONT color=#00ed0>"aA"</FONT>);
System.out.println(m10.find());
System.out.println(m10.find());
<FONT color=#0d00e0>//test COMMENTS</FONT>
System.out.println(<FONT color=#00ed0>"test COMMENTS"</FONT>);
Pattern p11=Pattern.compile(<FONT color=#00ed0>" a a #ccc"</FONT>,Pattern.COMMENTS);
Matcher m11=p11.matcher(<FONT color=#00ed0>"aa a a #ccc"</FONT>);
System.out.println(m11.find());
System.out.println(m11.find());
System.out.println(<FONT color=#00ed0>"test COMMENTS"</FONT>);
Pattern p12 = Pattern.compile(<FONT color=#00ed0>"(?x) a a #ccc"</FONT>);
Matcher m12 = p12.matcher(<FONT color=#00ed0>"aa a a #ccc"</FONT>);
System.out.println(m12.find());
System.out.println(m12.find());
<FONT color=#0d00e0>//test MULTILINE这个大家多试试参照我上面对多行模式的理解</FONT>
System.out.println(<FONT color=#00ed0>"test MULTILINE"</FONT>);
Pattern p13=Pattern.compile(<FONT color=#00ed0>"^.?"</FONT>,Pattern.MULTILINE|Pattern.DOTALL);
Matcher m13=p13.matcher(<FONT color=#00ed0>"helloohelloo,loveroo"</FONT>);
System.out.println(m13.find());
System.out.println(<FONT color=#00ed0>"start:"</FONT>+m13.start()+<FONT color=#00ed0>"end:"</FONT>+m13.end());
System.out.println(m13.find());
<FONT color=#0d00e0>//System.out.println("start:"+m13.start()+"end:"+m13.end());</FONT>
System.out.println(<FONT color=#00ed0>"test MULTILINE"</FONT>);
Pattern p14=Pattern.compile(<FONT color=#00ed0>"(?m)^hell.*oo$"</FONT>,Pattern.DOTALL);
Matcher m14=p14.matcher(<FONT color=#00ed0>"hello,Worldoo\nhello,loveroo"</FONT>);
System.out.println(m14.find());
System.out.println(<FONT color=#00ed0>"start:"</FONT>+m14.start()+<FONT color=#00ed0>"end:"</FONT>+m14.end());
System.out.println(m14.find());
<FONT color=#0d00e0>//System.out.println("start:"+m14.start()+"end:"+m14.end());</FONT>
System.out.println(<FONT color=#00ed0>"test MULTILINE"</FONT>);
Pattern p15=Pattern.compile(<FONT color=#00ed0>"^hell(.|[^.])*oo$"</FONT>);
Matcher m15=p15.matcher(<FONT color=#00ed0>"hello,Worldoo\nhello,loveroo"</FONT>);
System.out.println(m15.find());
System.out.println(<FONT color=#00ed0>"start:"</FONT>+m15.start()+<FONT color=#00ed0>"end:"</FONT>+m15.end());
System.out.println(m15.find());
<FONT color=#0d00e0>// System.out.println("start:"+m15.start()+"end:"+m15.end());</FONT>
<FONT color=#0d00e0>//test DOTALL</FONT>
System.out.println(<FONT color=#00ed0>"test DOTALL"</FONT>);
Pattern p16=Pattern.compile(<FONT color=#00ed0>"."</FONT>,Pattern.DOTALL);
Matcher m16=p16.matcher(<FONT color=#00ed0>"\n\r"</FONT>);
System.out.println(m16.find());
System.out.println(m16.find());
System.out.println(<FONT color=#00ed0>"test DOTALL"</FONT>);
Pattern p17=Pattern.compile(<FONT color=#00ed0>"."</FONT>);
Matcher m17=p17.matcher(<FONT color=#00ed0>"\n\r"</FONT>);
System.out.println(m17.find());
System.out.println(m17.find());
System.out.println(<FONT color=#00ed0>"test DOTALL"</FONT>);
Pattern p18=Pattern.compile(<FONT color=#00ed0>"(?s)."</FONT>);
Matcher m18=p18.matcher(<FONT color=#00ed0>"\n\r"</FONT>);
System.out.println(m18.find());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -