java中正则表达式运用实例(用String类的matches方法演示)
java中正则表达式运用实例(参看java中正则表达式运用详解):测试代码type="application/x-shockwave-flash"width="14"height="15"src="http://fhd001.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf"pluginspage="http://www.macromedia.com/go/getflashplayer"allowscriptaccess="always"quality="high"flashvars="clipboard=package%20test%3B%0A%2F**%0A%20*%20%E5%9C%A8String%E7%9A%84matches()%E6%96%B9%E6%B3%95%2Csplit()%E6%96%B9%E6%B3%95%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.%0A%20*%20%40author%20fhd001%0A%20*%2F%0Apublic%20class%20RegexTest%20%7B%0A%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%0A%09%09%2F*%0A%09%09%20*%20%E6%99%AE%E9%80%9A%E5%AD%97%E7%AC%A6%0A%09%09%20*%2F%0A%09%09String%20str1%20%3D%20%22abc45abc345%22%3B%0A%09%09String%5B%5Darr1%20%3D%20str1.split(%22abc%22)%3B%0A%09%09for%20(String%20string%20%3A%20arr1)%20%7B%0A%09%09%09System.out.print(string%2B%22--%3E%22)%3B%0A%09%09%7D%0A%09%09System.out.println()%3B%0A%09%09%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%E7%AE%80%E5%8D%95%E7%9A%84%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6%0A%09%09%20*%20%E5%9C%A8java%E4%B8%AD%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6%E6%97%B6%E4%B8%80%E5%AE%9A%E8%A6%81%E7%94%A8%E5%8F%8C%E5%8F%8D%E6%96%9C%E7%BA%BF%2C%E4%B8%A4%E4%B8%AA%E6%96%9C%E7%BA%BF%E8%BD%AC%E4%B9%89%E6%88%90%E4%B8%80%E4%B8%AA%E6%96%9C%E7%BA%BF%2C%0A%09%09%20*%20%E5%9C%A8%E7%94%A8%E8%BF%99%E4%B8%AA%E6%96%9C%E7%BA%BF%E5%AD%97%E7%AC%A6%E8%BD%AC%E4%B9%89%E9%82%A3%E4%BA%9B%E7%89%B9%E6%AE%8A%E5%AD%97%E7%AC%A6.%0A%09%09%20*%2F%0A%09%09String%20str2%20%3D%20%22%5E%24()%5B%5D%7B%7D.%3F%2B*%7C%22%3B%0A%09%09boolean%20flag%20%3D%20str2.matches(%22%5C%5C%5E%5C%5C%24%5C%5C(%5C%5C)%5C%5C%5B%5C%5C%5D%5C%5C%7B%5C%5C%7D%5C%5C.%5C%5C%3F%5C%5C%2B%5C%5C*%5C%5C%7C%22)%3B%0A%09%09System.out.println(flag)%3B%0A%09%09%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6%20%5CQ...%5CE.%0A%09%09%20*%2F%0A%09%09String%20str3%20%3D%20%22%5E%24()%5B%5D%7B%7D.%3F%2B*%7C99999%22%3B%0A%09%09boolean%20flag2%20%3D%20str3.matches(%22%5C%5CQ%5E%24()%5B%5D%7B%7D.%3F%2B*%7C%5C%5CE%5C%5Cd%7B5%7D%22)%3B%0A%09%09System.out.println(flag2)%3B%0A%09%09%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%E5%AD%97%E7%AC%A6%E9%9B%86%E5%90%88%0A%09%09%20*%2F%0A%09%09String%20str4%20%3D%20%22dfddri334%22%3B%0A%09%09boolean%20flag4%20%3D%20str4.matches(%22.%2B%22)%3B%0A%09%09System.out.println(flag4)%3B%0A%09%09%0A%09%09String%20str5%20%3D%20%22%23%24%25%5E*())%25%22%3B%0A%09%09boolean%20flag5%20%3D%20str5.matches(%22%5C%5CW%7B9%7D%22)%3B%0A%09%09System.out.println(flag5)%3B%0A%09%09%0A%09%09String%20str6%20%3D%20%224gffmdkekrhhr%22%3B%0A%09%09boolean%20flag6%20%3D%20str6.matches(%22%5C%5Cw%2B%22)%3B%0A%09%09System.out.println(flag6)%3B%0A%09%09%0A%09%09String%20str7%20%3D%20%22fjfdke%20%20%20%20eett%20%22%3B%0A%09%09boolean%20flag7%20%3D%20str7.matches(%22%5C%5Cw%2B%5C%5Cs%2B%5C%5Cw%7B4%7D%5C%5Cs%3F%22)%3B%0A%09%09System.out.println(flag7)%3B%0A%09%09%0A%09%09String%20str8%20%3D%20%22%20%20%20erefff%20%20%20%20%22%3B%0A%09%09boolean%20flag8%20%3D%20str8.matches(%22%5C%5Cs%2B%5C%5CS%2B%5C%5Cs%2B%22)%3B%0A%09%09System.out.println(flag8)%3B%0A%09%09%0A%09%09String%20str9%20%3D%20%22456776888%22%3B%0A%09%09boolean%20flag9%20%3D%20str9.matches(%22%5C%5Cd%2B%22)%3B%0A%09%09System.out.println(flag9)%3B%0A%09%09%0A%09%09%0A%09%09String%20str10%20%3D%20%22rtydfgrgwvr%20%20%22%3B%0A%09%09boolean%20flag10%20%3D%20str10.matches(%22%5C%5CD%2B%22)%3B%0A%09%09System.out.println(flag10)%3B%0A%09%09%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%E8%87%AA%E5%AE%9A%E4%B9%89%E5%AD%97%E7%AC%A6%E9%9B%86%E5%90%88%20%5B%20%5D%0A%09%09%20*%2F%0A%09%09String%20str11%20%3D%20%22fdfeetg%20%2034566%22%3B%0A%09%09boolean%20flag11%20%3D%20str11.matches(%22%5Bfdetg%5D%2B%5C%5Cs%2B%5B3-6%5D%2B%22)%3B%0A%09%09System.out.println(flag11)%3B%0A%09%09%0A%09%09%0A%09%09String%20str12%20%3D%20%22rtyuie%20%20%205768%22%3B%0A%09%09boolean%20flag12%20%3D%20str12.matches(%22%5B%5Eabcdf%5D%2B%5C%5Cs%2B%5B%5E1234%5D%2B%22)%3B%0A%09%09System.out.println(flag12)%3B%0A%09%09%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%E5%8C%B9%E9%85%8D%E6%AC%A1%E6%95%B0%E9%99%90%E5%AE%9A%E7%AC%A6%0A%09%09%20*%2F%0A%09%09%0A%09%09%2F%2F%E8%B4%AA%E5%A9%AA%E6%A8%A1%E5%BC%8F%0A%09%09String%20str13%20%3D%20%22ytreggcv454444444333%22%3B%0A%09%09boolean%20flag13%20%3D%20str13.matches(%22%5C%5Cw%7B20%7D%22)%3B%0A%09%09System.out.println(flag13)%3B%0A%09%09%0A%09%09boolean%20flag14%20%3D%20str13.matches(%22%5C%5Cw%7B10%2C21%7D%22)%3B%0A%09%09System.out.println(flag14)%3B%0A%09%09%0A%09%09boolean%20flag15%20%3D%20str13.matches(%22%5C%5Cw%7B18%2C%7D%22)%3B%0A%09%09System.out.println(flag15)%3B%0A%09%09%0A%09%09String%20str14%20%3D%20%224%22%3B%0A%09%09boolean%20flag16%20%3D%20str14.matches(%22%5C%5Cd%3F%22)%3B%0A%09%09System.out.println(flag16)%3B%0A%09%09%0A%09%09String%20str15%20%3D%20%22ddcvgt%22%3B%0A%09%09boolean%20flag17%20%3D%20str15.matches(%22%5C%5CD%2B%5C%5Cd%3F%22)%3B%0A%09%09System.out.println(flag17)%3B%0A%09%09%0A%09%09String%20str16%20%3D%20%22e33tf44t44t%22%3B%0A%09%09boolean%20flag18%20%3D%20str16.matches(%22%5C%5Cw%2B%5C%5CW*%22)%3B%0A%09%09System.out.println(flag18)%3B%0A%09%09%0A%09%09%2F%2F%E5%8B%89%E5%BC%BA%E6%A8%A1%E5%BC%8F(%E5%8F%AA%E4%B8%BE%E4%B8%80%E4%BE%8B)%0A%09%09String%20str17%20%3D%20%2234567ghjkkld%22%3B%0A%09%09boolean%20flag19%20%3D%20str17.matches(%22%5C%5Cd%7B2%2C7%7D%3F%5C%5Cw%7B8%2C11%7D%22)%3B%0A%09%09System.out.println(flag19)%3B%0A%09%09%0A%09%09%2F%2F%E5%8D%A0%E6%9C%89%E6%A8%A1%E5%BC%8F(%E5%8F%AA%E4%B8%BE%E4%B8%80%E4%BE%8B)%0A%09%09String%20str18%20%3D%20%2222222ddddd%22%3B%0A%09%09boolean%20flag20%20%3D%20str18.matches(%22%5C%5Cd%7B2%2C5%7D%2B%5C%5Cw%7B6%7D%22)%3B%0A%09%09System.out.println(flag20)%3B%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%E5%AD%97%E7%AC%A6%E8%BE%B9%E7%95%8C%0A%09%09%20*%2F%0A%09%09String%20str19%20%3D%20%22a444545rot44tm%22%3B%0A%09%09boolean%20flag21%20%3D%20str19.matches(%22%5Ea%5C%5Cw%2Bm%24%22)%3B%0A%09%09System.out.println(flag21)%3B%0A%09%09%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%E9%80%89%E6%8B%A9%E8%A1%A8%E8%BE%BE%E5%BC%8F%20xxx%20%7C%20xxx%0A%09%09%20*%2F%0A%09%09String%20str20%20%3D%20%22abc123abc%22%3B%0A%09%09boolean%20flag22%20%3D%20str20.matches(%22(abc%7C123)%7B3%7D%22)%3B%0A%09%09System.out.println(flag22)%3B%0A%09%09%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%E5%88%86%E7%BB%84%20(%20).%0A%09%09%20*%20%E4%BB%A5%E4%B8%8A%E4%BF%AE%E9%A5%B0%E7%AC%A6%E9%83%BD%E6%98%AF%E9%92%88%E5%AF%B9%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E8%BF%9B%E8%A1%8C%E4%BF%AE%E9%A5%B0.%E5%A6%82%E6%9E%9C%E8%A6%81%E5%AF%B9%E4%B8%80%E7%BB%84%E5%AD%97%E7%AC%A6%E8%BF%9B%E8%A1%8C%0A%09%09%20*%20%E4%BF%AE%E9%A5%B0%E5%B0%B1%E8%A6%81%E7%94%A8%E5%88%B0()%0A%09%09%20*%2F%0A%09%09String%20str21%20%3D%20%22123qwe123qwe%22%3B%0A%09%09boolean%20flag23%20%3D%20str21.matches(%22(123qwe)%7B2%7D%22)%3B%0A%09%09System.out.println(flag23)%3B%0A%09%09%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20%5B%5D%E4%B8%AD%E7%9A%84%E4%BA%A4%E9%9B%86%E4%B8%8E%E5%B9%B6%E9%9B%86%0A%09%09%20*%2F%0A%09%09String%20str22%20%3D%20%22abcdefgh1234567%22%3B%0A%09%09boolean%20flag24%20%3D%20str22.matches(%22%5Ba-z1-9%5D%2B%22)%3B%09%09%2F%2F%E5%B9%B6%E9%9B%86%0A%09%09System.out.println(flag24)%3B%0A%09%09%0A%09%09boolean%20flag25%20%3D%20str22.matches(%22%5Ba-z1-9%26%26%5Ba-h1-7%5D%5D%2B%22)%3B%20%2F%2F%E4%BA%A4%E9%9B%86%0A%09%09System.out.println(flag25)%3B%0A%09%7D%0A%7D%0A">
packagetest;
/**
*在String的matches()方法,split()方法中使用正则表达式.
*@authorfhd001
*/
publicclassRegexTest{
publicstaticvoidmain(String[]args){
/*
*普通字符
*/
Stringstr1="abc45abc345";
String[]arr1=str1.split("abc");
for(Stringstring:arr1){
System.out.print(string+"-->");
}
System.out.println();
/*
*简单的转义字符
*在java中转义字符时一定要用双反斜线,两个斜线转义成一个斜线,
*在用这个斜线字符转义那些特殊字符.
*/
Stringstr2="^$()[]{}.?+*|";
booleanflag=str2.matches("\\^\\$\\(\\)\\[\\]\\{\\}\\.\\?\\+\\*\\|");
System.out.println(flag);
/*
*转义字符\Q...\E.
*/
Stringstr3="^$()[]{}.?+*|99999";
booleanflag2=str3.matches("\\Q^$()[]{}.?+*|\\E\\d{5}");
System.out.println(flag2);
/*
*字符集合
*/
Stringstr4="dfddri334";
booleanflag4=str4.matches(".+");
System.out.println(flag4);
Stringstr5="#$%^*())%";
booleanflag5=str5.matches("\\W{9}");
System.out.println(flag5);
Stringstr6="4gffmdkekrhhr";
booleanflag6=str6.matches("\\w+");
System.out.println(flag6);
Stringstr7="fjfdkeeett";
booleanflag7=str7.matches("\\w+\\s+\\w{4}\\s?");
System.out.println(flag7);
Stringstr8="erefff";
booleanflag8=str8.matches("\\s+\\S+\\s+");
System.out.println(flag8);
Stringstr9="456776888";
booleanflag9=str9.matches("\\d+");
System.out.println(flag9);
Stringstr10="rtydfgrgwvr";
booleanflag10=str10.matches("\\D+");
System.out.println(flag10);
/*
*自定义字符集合[]
*/
Stringstr11="fdfeetg34566";
booleanflag11=str11.matches("[fdetg]+\\s+[3-6]+");
System.out.println(flag11);
Stringstr12="rtyuie5768";
booleanflag12=str12.matches("[^abcdf]+\\s+[^1234]+");
System.out.println(flag12);
/*
*匹配次数限定符
*/
//贪婪模式
Stringstr13="ytreggcv454444444333";
booleanflag13=str13.matches("\\w{20}");
System.out.println(flag13);
booleanflag14=str13.matches("\\w{10,21}");
System.out.println(flag14);
booleanflag15=str13.matches("\\w{18,}");
System.out.println(flag15);
Stringstr14="4";
booleanflag16=str14.matches("\\d?");
System.out.println(flag16);
Stringstr15="ddcvgt";
booleanflag17=str15.matches("\\D+\\d?");
System.out.println(flag17);
Stringstr16="e33tf44t44t";
booleanflag18=str16.matches("\\w+\\W*");
System.out.println(flag18);
//勉强模式(只举一例)
Stringstr17="34567ghjkkld";
booleanflag19=str17.matches("\\d{2,7}?\\w{8,11}");
System.out.println(flag19);
//占有模式(只举一例)
Stringstr18="22222ddddd";
booleanflag20=str18.matches("\\d{2,5}+\\w{6}");
System.out.println(flag20);
/*
*字符边界
*/
Stringstr19="a444545rot44tm";
booleanflag21=str19.matches("^a\\w+m$");
System.out.println(flag21);
/*
*选择表达式xxx|xxx
*/
Stringstr20="abc123abc";
booleanflag22=str20.matches("(abc|123){3}");
System.out.println(flag22);
/*
*分组().
*以上修饰符都是针对一个字符进行修饰.如果要对一组字符进行
*修饰就要用到()
*/
Stringstr21="123qwe123qwe";
booleanflag23=str21.matches("(123qwe){2}");
System.out.println(flag23);
/*
*[]中的交集与并集
*/
Stringstr22="abcdefgh1234567";
booleanflag24=str22.matches("[a-z1-9]+");//并集
System.out.println(flag24);
booleanflag25=str22.matches("[a-z1-9&&[a-h1-7]]+");//交集
System.out.println(flag25);
}}
结果代码type="application/x-shockwave-flash"width="14"height="15"src="http://fhd001.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf"pluginspage="http://www.macromedia.com/go/getflashplayer"allowscriptaccess="always"quality="high"flashvars="clipboard=--%3E45--%3E345--%3E%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0Afalse%0Atrue%0Atrue%0Atrue%0Atrue%0Atrue%0A">