欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java中正则表达式实例详解

程序员文章站 2024-03-01 20:55:10
java中正则表达式运用实例(参看java中正则表达式运用详解): 测试代码 package test; /** * 在string的match...

java中正则表达式运用实例(参看java中正则表达式运用详解):

测试代码

package test; 
/** 
 * 在string的matches()方法,split()方法中使用正则表达式. 
 * @author fhd001 
 */ 
public class regextest { 
 
  public static void main(string[] args) { 
 
    /* 
     * 普通字符 
     */ 
    string str1 = "abc45abc345"; 
    string[]arr1 = str1.split("abc"); 
    for (string string : arr1) { 
      system.out.print(string+"-->"); 
    } 
    system.out.println(); 
     
     
    /* 
     * 简单的转义字符 
     * 在java中转义字符时一定要用双反斜线,两个斜线转义成一个斜线, 
     * 在用这个斜线字符转义那些特殊字符. 
     */ 
    string str2 = "^$()[]{}.?+*|"; 
    boolean flag = str2.matches("//^//$//(//)//[//]//{//}//.//?//+//*//|"); 
    system.out.println(flag); 
     
     
    /* 
     * 转义字符 /q.../e. 
     */ 
    string str3 = "^$()[]{}.?+*|99999"; 
    boolean flag2 = str3.matches("//q^$()[]{}.?+*|//e//d{5}"); 
    system.out.println(flag2); 
     
     
    /* 
     * 字符集合 
     */ 
    string str4 = "dfddri334"; 
    boolean flag4 = str4.matches(".+"); 
    system.out.println(flag4); 
     
    string str5 = "#$%^*())%"; 
    boolean flag5 = str5.matches("//w{9}"); 
    system.out.println(flag5); 
     
    string str6 = "4gffmdkekrhhr"; 
    boolean flag6 = str6.matches("//w+"); 
    system.out.println(flag6); 
     
    string str7 = "fjfdke  eett "; 
    boolean flag7 = str7.matches("//w+//s+//w{4}//s?"); 
    system.out.println(flag7); 
     
    string str8 = "  erefff  "; 
    boolean flag8 = str8.matches("//s+//s+//s+"); 
    system.out.println(flag8); 
     
    string str9 = "456776888"; 
    boolean flag9 = str9.matches("//d+"); 
    system.out.println(flag9); 
     
     
    string str10 = "rtydfgrgwvr "; 
    boolean flag10 = str10.matches("//d+"); 
    system.out.println(flag10); 
     
     
    /* 
     * 自定义字符集合 [ ] 
     */ 
    string str11 = "fdfeetg 34566"; 
    boolean flag11 = str11.matches("[fdetg]+//s+[3-6]+"); 
    system.out.println(flag11); 
     
     
    string str12 = "rtyuie  5768"; 
    boolean flag12 = str12.matches("[^abcdf]+//s+[^1234]+"); 
    system.out.println(flag12); 
     
     
    /* 
     * 匹配次数限定符 
     */ 
     
    //贪婪模式 
    string str13 = "ytreggcv454444444333"; 
    boolean flag13 = str13.matches("//w{20}"); 
    system.out.println(flag13); 
     
    boolean flag14 = str13.matches("//w{10,21}"); 
    system.out.println(flag14); 
     
    boolean flag15 = str13.matches("//w{18,}"); 
    system.out.println(flag15); 
     
    string str14 = "4"; 
    boolean flag16 = str14.matches("//d?"); 
    system.out.println(flag16); 
     
    string str15 = "ddcvgt"; 
    boolean flag17 = str15.matches("//d+//d?"); 
    system.out.println(flag17); 
     
    string str16 = "e33tf44t44t"; 
    boolean flag18 = str16.matches("//w+//w*"); 
    system.out.println(flag18); 
     
    //勉强模式(只举一例) 
    string str17 = "34567ghjkkld"; 
    boolean flag19 = str17.matches("//d{2,7}?//w{8,11}"); 
    system.out.println(flag19); 
     
    //占有模式(只举一例) 
    string str18 = "22222ddddd"; 
    boolean flag20 = str18.matches("//d{2,5}+//w{6}"); 
    system.out.println(flag20); 
     
    /* 
     * 字符边界 
     */ 
    string str19 = "a444545rot44tm"; 
    boolean flag21 = str19.matches("^a//w+m$"); 
    system.out.println(flag21); 
     
     
    /* 
     * 选择表达式 xxx | xxx 
     */ 
    string str20 = "abc123abc"; 
    boolean flag22 = str20.matches("(abc|123){3}"); 
    system.out.println(flag22); 
     
     
    /* 
     * 分组 ( ). 
     * 以上修饰符都是针对一个字符进行修饰.如果要对一组字符进行 
     * 修饰就要用到() 
     */ 
    string str21 = "123qwe123qwe"; 
    boolean flag23 = str21.matches("(123qwe){2}"); 
    system.out.println(flag23); 
     
     
    /* 
     * []中的交集与并集 
     */ 
    string str22 = "abcdefgh1234567"; 
    boolean flag24 = str22.matches("[a-z1-9]+");    //并集 
    system.out.println(flag24); 
     
    boolean flag25 = str22.matches("[a-z1-9&&[a-h1-7]]+"); //交集 
    system.out.println(flag25); 
  } 
} 
 

结果代码

-->45-->345--> 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
true 
false 
true 
true 
true 
true 
true 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!