/******************************************
 *****JavaScript RegExp to Java************
 ***********By Huang Jin,2009/2/5***********
 ******************************************/
import java.util.regex.*;
public class RegExp {
/**
 * 属性global
 * 定义和用法
 * global 属性用于返回正则表达式是否具有标志 g。
 * 它声明了给定的正则表达式是否执行全局匹配。
 * 如果 g 标志被设置,则该属性为 true,否则为 false。
 * 语法
 * RegExpObject.global
 */
public final boolean global;
/**
 * 属性ignoreCase
 * 定义和用法
 * global 属性用于返回正则表达式是否具有标志 i。
 * 它声明了给定的正则表达式是否执行区分大小写的匹配。
 * 如果 i 标志被设置,则该属性为 true,否则为 false。
 * 语法
 * RegExpObject.ignoreCase
 */
public final boolean ignoreCase;
/**
 * 属性source
 * 定义和用法
 * lastIndex 属性用于规定下次匹配的起始位置。
 * 语法
 * RegExpObject.lastIndex;
 */
    public int lastIndex=0;
/**
 * 属性multiline
 * 定义和用法
 * multiline 属性用于返回正则表达式是否具有标志 m。
 * 它声明了给定的正则表达式是否以多行模式执行模式匹配。
 * 在这种模式中,如果要检索的字符串中含有换行符,^ 和 $ 锚除了匹配字符串的开头和结尾外还匹配每行的开头和结尾。
 * 语法
 * RegExpObject.multiline
 */
    public final boolean multiline;
/**
 * 属性source
 * 定义和用法
 * lastIndex 属性用于返回模式匹配所用的文本。
 * 该文本不包括正则表达式直接量使用的定界符,也不包括标志 g、i、m。
 * 语法
 * RegExpObject.source
 */
    public final String source;
    /**
     * 方法exec(String string)
     * 定义和用法
     * exec() 方法用于检索字符串中的正则表达式的匹配。
     * 语法
     * RegExpObject.exec(string)
     * 返回值
     * 返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。
     */
    /*
     * private变量
     */
    private Pattern regex;
    private int flag;
    /*private String getSource(String str){
     if(str.indexOf("\\")>0){
     str.replaceAll("\\","");
     System.out.println(1);
     }
     return str;
    }*/
    /**
     * 构造函数
     */
    RegExp(String regex){
     global=false;
     ignoreCase=false;
     multiline=false;
     //this.source=getSource(regex);
     //regex
     this.regex=Pattern.compile(regex);
     this.source=this.regex.toString();
    }
   RegExp(String regex,String param){
       //初始化param
   //当param为“g”时情况特殊,使用compile(regex);
   //this.source=getSource(regex);
   
   if(param.indexOf("g")>=0 && param.indexOf("i")<0 && param.indexOf("m")<0) {
    global=true;ignoreCase=false;multiline=false;
    this.regex=Pattern.compile(regex);
    this.source=this.regex.toString();
    return;
    }
       else if(param.indexOf("i")>=0 && param.indexOf("g")<0 && param.indexOf("m")<0) {
     global=false;ignoreCase=true;multiline=false;flag=Pattern.CASE_INSENSITIVE;
     }
     else if(param.indexOf("m")>=0 && param.indexOf("i")<0 && param.indexOf("g")<0){
     global=false;ignoreCase=false;multiline=true;flag=Pattern.MULTILINE;
     }
     else if(param.indexOf("g")>=0 && param.indexOf("i")>=0 && param.indexOf("m")<0) {
     global=true;ignoreCase=true;multiline=false;flag=Pattern.CASE_INSENSITIVE;
     }
     else if(param.indexOf("g")>=0 && param.indexOf("i")<0 && param.indexOf("m")>=0) {
     global=true;ignoreCase=false;multiline=true;flag=Pattern.MULTILINE;
     }
     else if(param.indexOf("g")<0 && param.indexOf("i")>=0 && param.indexOf("m")>=0) {
     global=false;ignoreCase=true;multiline=true;flag=Pattern.CASE_INSENSITIVE|Pattern.MULTILINE;
     }
     else{
     global=true;ignoreCase=true;multiline=true;flag=Pattern.CASE_INSENSITIVE|Pattern.MULTILINE;
     }   
     //regex
     this.regex=Pattern.compile(regex,flag);
     this.source=this.regex.toString();
    }
    
    public String exec(String str){
/*
     Matcher matcher=regex.matcher(str);
     if(matcher.find()){
     MatchResult result=matcher.toMatchResult();
     return str.substring(result.start(), result.end());
     }
     else
     return null;
*/
     if(global){
     Matcher matcher=regex.matcher(str);
     matcher=matcher.region(lastIndex, str.length());
         if(matcher.find()){
     MatchResult result=matcher.toMatchResult();
     lastIndex=result.end()+1;
     return str.substring(result.start(), result.end());
     }
     else {
     lastIndex=0;
     return null;
     }
    
     }
     else{
     Matcher matcher=regex.matcher(str);
     if(matcher.find()){
     MatchResult result=matcher.toMatchResult();
     return str.substring(result.start(), result.end());
     }
     else
     return null;
     }
    }
    /**
     * 方法test(String string)
     * 定义和用法
     * test() 方法用于检测一个字符串是否匹配某个模式.
     * 语法
     * RegExpObject.test(string)
     * 返回值
     * 如果字符串 string 中含有与 RegExpObject 匹配的文本,则返回 true,否则返回 false。
     */
    public boolean test(String str){
     return regex.matcher(str).find();
    }
    /*-----------------------Main()-------For test-----------------------------
    public static void main(String []args){
     RegExp reg=new RegExp("adb+","g");
     System.out.println(reg.test("adbbbbdddadbdddddd"));
     System.out.println("result:"+reg.exec("adbbbbaaadbba")+reg.exec("adbbbbaaadbba")+reg.exec("adbbbbaaadbba"));
    }
    */
    public static void main(String []args){
     RegExp reg=new RegExp("adb{1,3}","g");
     System.out.println(reg.test("adbbbbdddadbdddddd"));
     System.out.println("result:"+reg.exec("adbbbbaaadbba")+reg.exec("adbbbbaaadbba")+reg.exec("adbbbbaaadbba"));
    }
}

解决方案 »

  1.   

    /******************************************
     *****JavaScript RegExp to Java************
     ***********By Huang Jin,2009/2/5***********
     ******************************************/
    import java.util.regex.*;
    public class RegExp {
    /**
     * 属性global
     * 定义和用法
     * global 属性用于返回正则表达式是否具有标志 g。
     * 它声明了给定的正则表达式是否执行全局匹配。
     * 如果 g 标志被设置,则该属性为 true,否则为 false。
     * 语法
     * RegExpObject.global
     */
    public final boolean global;
    /**
     * 属性ignoreCase
     * 定义和用法
     * global 属性用于返回正则表达式是否具有标志 i。
     * 它声明了给定的正则表达式是否执行区分大小写的匹配。
     * 如果 i 标志被设置,则该属性为 true,否则为 false。
     * 语法
     * RegExpObject.ignoreCase
     */
    public final boolean ignoreCase;
    /**
     * 属性source
     * 定义和用法
     * lastIndex 属性用于规定下次匹配的起始位置。
     * 语法
     * RegExpObject.lastIndex;
     */
        public int lastIndex=0;
    /**
     * 属性multiline
     * 定义和用法
     * multiline 属性用于返回正则表达式是否具有标志 m。
     * 它声明了给定的正则表达式是否以多行模式执行模式匹配。
     * 在这种模式中,如果要检索的字符串中含有换行符,^ 和 $ 锚除了匹配字符串的开头和结尾外还匹配每行的开头和结尾。
     * 语法
     * RegExpObject.multiline
     */
        public final boolean multiline;
    /**
     * 属性source
     * 定义和用法
     * lastIndex 属性用于返回模式匹配所用的文本。
     * 该文本不包括正则表达式直接量使用的定界符,也不包括标志 g、i、m。
     * 语法
     * RegExpObject.source
     */
        public final String source;
        /**
         * 方法exec(String string)
         * 定义和用法
         * exec() 方法用于检索字符串中的正则表达式的匹配。
         * 语法
         * RegExpObject.exec(string)
         * 返回值
         * 返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。
         */
        /*
         * private变量
         */
        private Pattern regex;
        private int flag;
        /*private String getSource(String str){
         if(str.indexOf("\\")>0){
         str.replaceAll("\\","");
         System.out.println(1);
         }
         return str;
        }*/
        /**
         * 构造函数
         */
        RegExp(String regex){
         global=false;
         ignoreCase=false;
         multiline=false;
         //this.source=getSource(regex);
         //regex
         this.regex=Pattern.compile(regex);
         this.source=this.regex.toString();
        }
       RegExp(String regex,String param){
           //初始化param
       //当param为“g”时情况特殊,使用compile(regex);
       //this.source=getSource(regex);
       
       if(param.indexOf("g")>=0 && param.indexOf("i")<0 && param.indexOf("m")<0) {
        global=true;ignoreCase=false;multiline=false;
        this.regex=Pattern.compile(regex);
        this.source=this.regex.toString();
        return;
        }
           else if(param.indexOf("i")>=0 && param.indexOf("g")<0 && param.indexOf("m")<0) {
         global=false;ignoreCase=true;multiline=false;flag=Pattern.CASE_INSENSITIVE;
         }
         else if(param.indexOf("m")>=0 && param.indexOf("i")<0 && param.indexOf("g")<0){
         global=false;ignoreCase=false;multiline=true;flag=Pattern.MULTILINE;
         }
         else if(param.indexOf("g")>=0 && param.indexOf("i")>=0 && param.indexOf("m")<0) {
         global=true;ignoreCase=true;multiline=false;flag=Pattern.CASE_INSENSITIVE;
         }
         else if(param.indexOf("g")>=0 && param.indexOf("i")<0 && param.indexOf("m")>=0) {
         global=true;ignoreCase=false;multiline=true;flag=Pattern.MULTILINE;
         }
         else if(param.indexOf("g")<0 && param.indexOf("i")>=0 && param.indexOf("m")>=0) {
         global=false;ignoreCase=true;multiline=true;flag=Pattern.CASE_INSENSITIVE|Pattern.MULTILINE;
         }
         else{
         global=true;ignoreCase=true;multiline=true;flag=Pattern.CASE_INSENSITIVE|Pattern.MULTILINE;
         }   
         //regex
         this.regex=Pattern.compile(regex,flag);
         this.source=this.regex.toString();
        }
        
        public String exec(String str){
    /*
         Matcher matcher=regex.matcher(str);
         if(matcher.find()){
         MatchResult result=matcher.toMatchResult();
         return str.substring(result.start(), result.end());
         }
         else
         return null;
    */   try{
         if(global){
         Matcher matcher=regex.matcher(str);
         matcher=matcher.region(lastIndex, str.length());
              if(matcher.find()){
         MatchResult result=matcher.toMatchResult();
         lastIndex=result.end()+1;
         return str.substring(result.start(), result.end());
         }
         else {
         lastIndex=0;
         matcher.reset();
         return null;
         }
        
         }
         else{
         Matcher matcher=regex.matcher(str);
         if(matcher.find()){
         MatchResult result=matcher.toMatchResult();
         return str.substring(result.start(), result.end());
         }
         else
         return null;
         }
         }
    catch(Exception e){
    //e.printStackTrace(); --for test
    lastIndex=0;
    //matcher.reset();
    return null;
    }
        }
        /**
         * 方法test(String string)
         * 定义和用法
         * test() 方法用于检测一个字符串是否匹配某个模式.
         * 语法
         * RegExpObject.test(string)
         * 返回值
         * 如果字符串 string 中含有与 RegExpObject 匹配的文本,则返回 true,否则返回 false。
         */
        public boolean test(String str){
         return regex.matcher(str).find();
        }
        /*-----------------------Main()-------For test-----------------------------
        public static void main(String []args){
         RegExp reg=new RegExp("adb+","g");
         System.out.println(reg.test("adbbbbdddadbdddddd"));
         System.out.println("result:"+reg.exec("adbbbbaaadbba")+reg.exec("adbbbbaaadbba")+reg.exec("adbbbbaaadbba"));
        }
        */
        public static void main(String []args){
         RegExp reg=new RegExp("adb","g");
         System.out.println(reg.test("adbbbbdddadbdddddd"));
         System.out.println("result:"+reg.exec("adb")+reg.exec("adb")+reg.exec("adb"));
        }
    }