各位大哥,小弟对正则没有学过,想请教一个正则表面式.代码如下:String str = "hello{name},how much{keyword_1},{url},{}";String [] splits = str.split("请问这个正则怎么写");我想请教的是上面这个正则怎么去写,能够匹配语句中的以{开头,并且以}结尾的关键字,万分感谢.我这样写"\\{*\\}"不能匹配到}谢谢.我希望匹配出来后的数组是这样的{"{name}","{keyword_1}","{url}"}

解决方案 »

  1.   

    正则不行吧,需要自己截取,String.indexOf, String.substring
      

  2.   

    Pattern pattern2 = Pattern.compile("\\{[^}]+\\}");
      

  3.   

    public class Try
    {
        public static void main(String[] args)
        {
            String str = "hello{name},how much{keyword_1},{url},{}";
            
            Pattern pattern2 = Pattern.compile("\\{[^}]+\\}");
            
            Matcher m2 = pattern2.matcher(str);
            
            while (m2.find())
            {
                String tmp = m2.group().replaceAll("[\\{\\}]", "");
                System.out.println(tmp);
            }
        }
    }
      

  4.   

    按照Try类运行可以,但是用split来就不能分隔出来了
      

  5.   


    String str = "hello{name},how much{keyword_1},{url},{}";
    String[] ss = str.split("[\\w[\\s[\\,]]]+(?=\\{\\w+\\})|(?:\\{\\s?\\})|[\\,]+");
    for(String s:ss) 
    System.out.println(s);数组第[0]是空的,原因不明,自己过滤掉就好了。