JAVA 
String str="{\"music\":{\"count\":0,\"returncode\":\"000000\"}}";如上是一段字符串想求一个使用java 正则表达式 得到上面的returncode  对应的值

解决方案 »

  1.   

    你都知道是returncode 了用indexOf和subString不就可以了
      

  2.   

    为啥非要用正则,正则效率并不高,而且说实话不好写,调试也得花时间
    给段代码,参考下(非正则)
    class T {
    public static void main(String args[]) {
    String str = "{\"music\":{\"count\":0,\"returncode\":\"000000\"}}";
    String tem[] = str.split(":");
    String tem1[] = tem[tem.length - 1].split("\"");
    System.out.println(tem1[1]);
    }
    }
      

  3.   

    为啥非要用正则,正则效率并不高,而且说实话不好写,调试也得花时间 
    给段代码,参考下(非正则) ?
    完全不同意~~正则表达是效率非常高,wait,我帮你写写
      

  4.   


    var str ="{\"music\":{\"count\":0,\"returncode\":\"000000\"}}"; ;
    var reg =  /{"\w+?":{"\w+?":\d,"(\w+?)":"\d+?"}}/gi;
      reg.test(str);
      window.alert(RegExp.$1);
      

  5.   


    public class Test
    {
        public static void main(String[] args)
        {        String regex = "\"returncode\":\".+\"";
            
            String str="{\"music\":{\"count\":0,\"returncode\":\"000000\"}}"; 
            
            Pattern pattern = Pattern.compile(regex);
            
            Matcher matcher = pattern.matcher(str);
            
            boolean rs = matcher.find();
            
            if (rs)
            {
                // 匹配的字符串存放在matcher.group(i)中,其中i最大值为m.groupCount()
                for(int i = 0; i <= matcher.groupCount(); i++)
                {
                    System.out.println(matcher.group(i)); // 输出结果为:"returncode":"000000",如果只想要"000000",自己想办法吧
                }
            }
            else {
                System.out.println(rs);
            }
        }
    }
      

  6.   

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;String str="{\"music\":{\"count\":0,\"returncode\":\"000000\"}}";
    Pattern ptn = Pattern.compile("returncode.*?([0-9]+)");
    Matcher mat = ptn.matcher(str);
    while(mat.find())
    {
    System.out.println(mat.group(1));
    }
      

  7.   

    直接得到"returncode"里面的值(不包含"returncode"本身):(?<="returncode":").*(?=")