日期:2014-05-19  浏览次数:20613 次

怎么用正则表达式截取字符串,急!!!
字符串如下所示:


U#:zhang_san#:ASSIGN#:2#:0#:0#:0#:#:#:#:#;U#:wang_wu#:ASSIGN#:2#:0#:0#:0#:#:#:#:#;


我想把zhang_san和wang_wu截取出来

------解决方案--------------------
Java code

        String text = "U#:zhang_san#:ASSIGN#:2#:0#:0#:0#:#:#:#:#;U#:wang_wu#:ASSIGN#:2#:0#:0#:0#:#:#:#:#;";
        Matcher m = Pattern.compile("U#:([^#]+)#").matcher(text);
        StringBuilder sb = new StringBuilder();
        while(m.find()){
            sb.append(m.group(1)).append(":");
        }
        if(sb.length()>1)
            sb.deleteCharAt(sb.length()-1);
        System.out.println(sb.toString());

------解决方案--------------------
探讨

Java code

String text = "U#:zhang_san#:ASSIGN#:2#:0#:0#:0#:#:#:#:#;U#:wang_wu#:ASSIGN#:2#:0#:0#:0#:#:#:#:#;";
Matcher m = Pattern.compile("U#:([^#]+)#").matcher(text);
Strin……