String str = "abcdefg ${Show_你的名字}";
我想得到 rtnStr = "Show_你的名字";
使用正则表达式,该怎么写?

解决方案 »

  1.   

    public static void main(String[] args) {
      String str = "abcdefg ${Show_你的名字}";
      Pattern p = Pattern.compile("(.*\\$\\{)(.*)(\\})(.*)");
      Matcher m = p.matcher(str);
      String rtnStr = "";
      if(m.find()){
        rtnStr = m.group(2);
      }
      System.out.println(rtnStr);
    }不知道这样可以吗,这样的前提是“show_你的名字”是 ${...}形式的,否则需要修改代码。
      

  2.   

    谢谢楼上的XD,呵呵~~
    正则表达式还需up!up!up!
      

  3.   

    public static void main(String[] args) {
      String str = "abcdefg ${Show_你的名字1} abcdefg ${Show_你的名字2} abcdefg ${Show_你的名字3} adfa";
      Pattern p = Pattern.compile("(\\$\\{)([^\\$.]*)(\\})");
      Matcher m = p.matcher(str);
      List<String> names = new ArrayList<String>();
      while(m.find()){
        names.add(m.group(2));
      }
    }
      

  4.   

    这样行不?
    \\$\\{([^\\}]+)
    ......
    group(1)