String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
如果把字符串里的()里面的password值全读出来?

解决方案 »

  1.   


            String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
            String regex = "\\(([^\\(\\)]+)\\)";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(addr);
            while (m.find())
                System.out.println(m.group(1));
      

  2.   

    public static void main(String[] args) {
    String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
    Matcher m = Pattern.compile("(\\((.+?)\\))").matcher(addr);
    while (m.find()) {
    System.out.println(m.group(2));
    }
    }
      

  3.   

    你可以用substring()和indexof()两个方法得到,大概如下:
    String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
    int beginIndex = addr.indexOf("(");
    int endIndex = addr.indexOf(")");
    String password1 = addr.substring(beginIndex, endIndex);//得到password1String addr1 = addr.substring(endIndex+1);
    //此时addr1 ="user2(password2),user3(password3),user4(password4),";重复上面的方法,就可以得到password2,以此类推
      

  4.   

    试试这个
    public static void main(String[] args) {
            
            String regex = "(?<=\\()\\w+(?=\\))";//或者String regex = "(?<=\\()(.+?)(?=\\))",看你的password中是否含有非字母、数字、下划线的字符而定。
            String str;
            String strOri = "user1(password1),user2(password2),user3(password3),user4(password4)";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(strOri);
            while(matcher.find())
            {
                str = matcher.group();
                System.out.println(str);
            }
        }
      

  5.   

    可以呀,用正则匹配出来放到数组里面就可以了。就在那个while循环里面放进去。
      

  6.   

    public static void main(String[] args) {
        String addr = "user1(password1),user2(password2),user3(password3),user4(password4),";
        String[] str = addr.split(",");
        for (int i = 0; i < str.length; i++) {
          str[i] = str[i].substring(str[i].indexOf("(")+1, str[i].length() - 1);
        }
        for (int i = 0; i < str.length; i++) {
          System.out.println(str[i]);
        }
      }