各位大哥,小弟对正则没有学过,想请教一个正则表面式.代码如下:String str = "hello{name},how much{keyword_1},{url},{}";String [] splits = str.split("请问这个正则怎么写");我想请教的是上面这个正则怎么去写,能够匹配语句中的以{开头,并且以}结尾的关键字,万分感谢.我这样写"\\{*\\}"不能匹配到}谢谢.我希望匹配出来后的数组是这样的{"{name}","{keyword_1}","{url}"}
调试欢乐多
{
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);
}
}
}
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]是空的,原因不明,自己过滤掉就好了。