rt,大侠们谢了!!怎么我只能提取第一个/*到最后一个*/之间的注释(1-4),不能提取第一个/*到最后一个*/之间的注释,比如1-2??
/*xxxxx   1
xxxxx
xxxxx*/   2/*fsdfsdf 3
dfsdfsdf
*/       4

解决方案 »

  1.   

    使用 "非贪婪模式":
    /\*.*?\*/详细说明:
    http://www.regexlab.com/zh/regref.htm
      

  2.   

    对,就是贪婪问题。你看一下JDBMonitor中DataBaseDBListener中init方法中
    //not dburl=(.+);?(user=.*;password=.*;)?(logtable=.+)?
    //because "+" in regular express is greedy.so add ? after + to make it lazy   
    Pattern patAll = Pattern.compile("dburl=(.+?);?(user=.*;password=.*;)?(logtable=.+)?");
    Matcher matAll = patAll.matcher(arg);
    这就是为什么要用.+?
      

  3.   

    第一点: 要使用非贪婪模式,详情请见:
    http://www.regexlab.com/zh/regref.htm
    第二点: 在源代码中,"字符串" 和 "注释" 是可以相互包含的,换句话说/*  "aaa"  */    这是一行注释"  /*  */  "     这是一行字符串
    "   /*   "
    "   */   "       这是字符串因此,分析注释离不开分析字符串。因为 "字符串内的注释不是注释",而 "注释内的字符串不是字符串"