对,就是贪婪问题。你看一下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); 这就是为什么要用.+?
/\*.*?\*/详细说明:
http://www.regexlab.com/zh/regref.htm
//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);
这就是为什么要用.+?
http://www.regexlab.com/zh/regref.htm
第二点: 在源代码中,"字符串" 和 "注释" 是可以相互包含的,换句话说/* "aaa" */ 这是一行注释" /* */ " 这是一行字符串
" /* "
" */ " 这是字符串因此,分析注释离不开分析字符串。因为 "字符串内的注释不是注释",而 "注释内的字符串不是字符串"