求一正则表达式,高手帮忙看下(按时结贴)
(1) "select avg(age),sex from a where sex> 20 group by Sex "
(2) "select avg(age),sex from a where sex> 20 group by Sex,city "
字符串如上(格式基本固定,只是group by 后可能有多个分组),要求得到()里的内容和group by后的内容
比如上面:(1)得出 age sex (2) 得出age sex city
------解决方案--------------------没看懂,不好意思
------解决方案--------------------不会,帮顶一下吧~
------解决方案--------------------import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test {
public static void main(String[] args) {
Pattern pattern1 = Pattern.compile( "\\(.*\\) " );
Pattern pattern2 = Pattern.compile( "(group by).* " );
Matcher matcher1 = pattern1.matcher( "select avg(age),sex from a where sex> 20 group by Sex " );
Matcher matcher2 = pattern2.matcher( "select avg(age),sex from a where sex> 20 group by Sex " );
while (matcher1.find( )) {
System.out.println( matcher1.group( 0 ).substring( 1, matcher1.group( 0 ).length( )-1 ) );
}
while (matcher2.find( )) {
System.out.println( matcher2.group( 0 ).substring( "group by ".length( ) ) );
}
}
}