现在的需求是这样的:不如说我这里有一个sql语句:select a as {area},b as {time} from Users,后台取到这个string 后,用什么办法把这个string中含有"{}"中的数据取到,这里只是写了两个"{}",可能有多个,我想是用substring的方法,可是这个办法有点笨。后来又想到java中的正则表达式,但是有点不会用,希望大家帮个忙。处理下……。期待中。谢谢各位了

解决方案 »

  1.   

    这也没什么效率问题,与其花时间研究正则不如就substring吧
      

  2.   

    bluefcxt : 用substring这样的效率很低啊,有没有好的办法。
      

  3.   

    效率还可以吧,我都是用SUBSTRING的,数据量也不小,还可以呀,你试一下啊先
      

  4.   

    substring如果是stringbuilder或者stringbuffer的话效率还是不算低的. 
    直接用string效率才会比较低下.研究正则的话使用起来比较麻烦. 反正本人到目前为止还是没太懂正则. 你用stringbuilder试试.用string的split用正则表达式拆成数组也成.  数组效率也挺高的.
      

  5.   

    import org.apache.oro.text.regex.*;public class Test { public static void main(String[] args) {
    cycleMatch("select a as {area},b as {time} from Users","\\{[a-z]*\\}",0);
    }

    private static void cycleMatch(String inputValue, String reg, final int groupid) {
    PatternCompiler compile = new Perl5Compiler();
    try {
    Pattern p = compile.compile(reg,Perl5Compiler.CASE_INSENSITIVE_MASK);
    PatternMatcherInput input = new PatternMatcherInput(inputValue);
    Perl5Matcher pm = new Perl5Matcher();
    MatchResult result = null;
    int i = 0;
    while (pm.contains(input, p)) {
    result = pm.getMatch();
    System.out.println("result =" + result.group(groupid));
    input.setBeginOffset(result.length());
    i++;
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    }
    确认后请给分,谢谢
      

  6.   

    terryzhou2k : 请问上面的方法你测试了么?怎么我在我的机子上运行时:就出现  javaw.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。错误。
    麻烦你再帮看看。