求个解决方案

leiru 2009-10-30 03:10:23
String a="我叫{名字},是干{什么}的";
我想把{}中的字符放到一个数组中。
求个正则表达式,或者其他的解决办法。。

在线等,急求。
...全文
55 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Busing 2009-11-07
  • 打赏
  • 举报
回复
\\{([^\\{\\}]*)\\}
guodong66 2009-10-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 landor2004 的回复:]
Java codepublicstaticvoid main(String []args) {
String str="我叫{名字},是干{什么}的";
Pattern p2= Pattern.compile("\\{([^\\{\\}]*)\\}");
Matcher m2=p2.matcher(str);while(m2.find()){
System.out.println(m2.group(1));
}
}
[/Quote]

正解,顶。
rookie001 2009-10-30
  • 打赏
  • 举报
回复
太粗心啦
regx = "\\{[^\\{^\\}]*\\}
closewbq 2009-10-30
  • 打赏
  • 举报
回复

public static void main(String[] args) {
String str = "我叫{名字},是干{什么}的";
Pattern pattern=Pattern.compile("\\{([^{|}]*)\\}");
Matcher m=pattern.matcher(str);
List<String> list=new ArrayList<String>();
while(m.find()){
list.add(m.group(1));
}
System.out.println(list);
}
Landor2004 2009-10-30
  • 打赏
  • 举报
回复
	public static void main(String []args) {
String str="我叫{名字},是干{什么}的";
Pattern p2 = Pattern.compile("\\{([^\\{\\}]*)\\}");
Matcher m2 =p2.matcher(str);
while(m2.find()){
System.out.println(m2.group(1));
}
}
rookie001 2009-10-30
  • 打赏
  • 举报
回复
import java.util.regex.*;

public class Test {

public static void main(String[] args) {
String a="我叫{名字},是干{什么}的";
String regx = "\\{[^{^}]*\\}";
Pattern p = Pattern.compile(regx);
Matcher m = p.matcher(a);
String []str = new String[10];
int index = 0;
String tmp = null;
while(m.find()){
tmp=m.group();
str[index++] = tmp.replaceAll("[{}]{1}", "");
}
for(int i=0;i<index;i++) {
System.out.println(str[i]);
}
}

}

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧