请教大伙一个问题:关于怎么解析一个string中含有特殊字符的问题。

xyz2011 2008-12-02 09:08:18
现在的需求是这样的:不如说我这里有一个sql语句:select a as {area},b as {time} from Users,后台取到这个string 后,用什么办法把这个string中含有"{}"中的数据取到,这里只是写了两个"{}",可能有多个,我想是用substring的方法,可是这个办法有点笨。后来又想到java中的正则表达式,但是有点不会用,希望大家帮个忙。处理下……。期待中。谢谢各位了
...全文
136 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyz2011 2008-12-02
  • 打赏
  • 举报
回复
terryzhou2k : 请问上面的方法你测试了么?怎么我在我的机子上运行时:就出现 javaw.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。错误。
麻烦你再帮看看。
terryzhou2k 2008-12-02
  • 打赏
  • 举报
回复
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();
}
}
}


确认后请给分,谢谢
havelock 2008-12-02
  • 打赏
  • 举报
回复
substring如果是stringbuilder或者stringbuffer的话效率还是不算低的.
直接用string效率才会比较低下.

研究正则的话使用起来比较麻烦. 反正本人到目前为止还是没太懂正则.

你用stringbuilder试试.

用string的split用正则表达式拆成数组也成. 数组效率也挺高的.
xyz2011 2008-12-02
  • 打赏
  • 举报
回复
你说的这个方法我在第一次做的时候就是这么来写的,也实现了,可是很不爽。想找更好的办法嘞,呵呵,大伙帮忙想想。
junying2yu 2008-12-02
  • 打赏
  • 举报
回复
效率还可以吧,我都是用SUBSTRING的,数据量也不小,还可以呀,你试一下啊先
xyz2011 2008-12-02
  • 打赏
  • 举报
回复
bluefcxt : 用substring这样的效率很低啊,有没有好的办法。
terryzhou2k 2008-12-02
  • 打赏
  • 举报
回复
给你MAIL了
terryzhou2k 2008-12-02
  • 打赏
  • 举报
回复
正则或者INDEXOF
bluefcxt 2008-12-02
  • 打赏
  • 举报
回复
这也没什么效率问题,与其花时间研究正则不如就substring吧

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧