现在的需求是这样的:不如说我这里有一个sql语句:select a as {area},b as {time} from Users,后台取到这个string 后,用什么办法把这个string中含有"{}"中的数据取到,这里只是写了两个"{}",可能有多个,我想是用substring的方法,可是这个办法有点笨。后来又想到java中的正则表达式,但是有点不会用,希望大家帮个忙。处理下……。期待中。谢谢各位了
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(); } } } 确认后请给分,谢谢
直接用string效率才会比较低下.研究正则的话使用起来比较麻烦. 反正本人到目前为止还是没太懂正则. 你用stringbuilder试试.用string的split用正则表达式拆成数组也成. 数组效率也挺高的.
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();
}
}
}
确认后请给分,谢谢
麻烦你再帮看看。