匹配固定字符开头,固定字符结尾的正则表达式写法

djs36 2009-12-31 01:22:45
比如一个字符串
今天{}早上{xxx}在{yyy}吃了一个{zzz}.


我想匹配出
{}
{xxx}
{yyy}
{zzz}


这四个,我试过 {.+?},但是匹配不出第一个{}

...全文
1520 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
苍蝇①号 2009-12-31
  • 打赏
  • 举报
回复
	public static void main(String []args){

Pattern p = Pattern.compile("(\\{[^}]*\\})");
String str = "今天{}早上{xxx}在{yyy}吃了一个{zzz}. ";
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group(1));
}


}
zhuzeitou 2009-12-31
  • 打赏
  • 举报
回复
额,貌似测试的久了些……和#5发重了
zhuzeitou 2009-12-31
  • 打赏
  • 举报
回复
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {
public static void main(String[] args) {
String regex = "\\{.*?\\}";
String input = "今天{}早上{xxx}在{yyy}吃了一个{zzz}.";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(m.group());
}
}
}


{}
{xxx}
{yyy}
{zzz}
若鱼1919 2009-12-31
  • 打赏
  • 举报
回复 1

public class RegTest{
public static void main(String rags[]){
String source="今天{}早上{xxx}在{yyy}吃了一个{zzz}.";
java.util.regex.Pattern p=java.util.regex.Pattern.compile("\\{.*?\\}");
java.util.regex.Matcher m=p.matcher(source);
while(m.find()){
System.out.println(m.group());
}
}
}
mar_xxy 2009-12-31
  • 打赏
  • 举报
回复
confirm("今天{}早上{xxx}在{yyy}吃了一个{zzz}".match(/{\w*}/g));
djs36 2009-12-31
  • 打赏
  • 举报
回复
好像无效.
snoowball 2009-12-31
  • 打赏
  • 举报
回复
改一下:
{[^}]*?}
[Quote=引用 1 楼 snoowball 的回复:]
{[^}]*}
[/Quote]
snoowball 2009-12-31
  • 打赏
  • 举报
回复
{[^}]*}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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