Java code public static void main(String[] args) { String source = " first {test} is here,two {test2} is here! " ; String find = " \\u007B.*\\u007D " ; Pattern pattern = Pattern.compile(find); Matcher matcher = pattern.matcher(source); while (matcher.find()) { System.out.println(matcher.group()); } } 我要实现的是将{test}和{test2}提取出来。但是用 Java code String find = " \\u007B.*\\u007D " ; 获取出来的是 {test} is here,two {test2} 貌似获取的是第一个{和最后一个}。我怎么能用正则去提取每一对{}的呢? 因为这个字符串的内容不是固定的。所以无法使用类似下面的方式解决: Java code String input = " first {test} is here,two {test2} is here! " ; Scanner scanner = new Scanner (input); scanner.findInLine ( " first (.+) is here,two (.+) is here! " ); MatchResult result = scanner.match(); 对我有用[0]丢个板砖[0]引用举报管理TOP 回复次数: 7<!-- | 浏览次数: 75--> huntor(逸飞)等 级: #1楼 得分:5回复于:2010-08-24 14:10:49 Java code (\\{ ^ \\} + \\}) 对我有用[0]丢个板砖[0]引用举报管理TOP 精华推荐: 人生12个经典领悟 zhaolinger2(询问者)等 级: #2楼 得分:5回复于:2010-08-24 14:11:15 "\\u007B.*?\\u007D"; 对我有用[0]丢个板砖[0]引用举报管理TOP 精华推荐: 关于线程同步的。。。看看错在哪 zhaolinger2(询问者)等 级: #3楼 得分:5回复于:2010-08-24 14:13:38 普及一下“非贪婪匹配”的知识。 一般情况下我们做的都是贪婪匹配,即能匹配到的最长的字符串。也就是你现在遇到的问题,把第一个左括号和第二个右括号中的大段匹配出来。 而这个时候需要用到非贪婪匹配,也就是,从左到右匹配,只要遇到符合要求的字符串,无论它是否是最长的,都把它拿出来当做匹配结果。 使用方法是在表示多个匹配的通配符,如*,+,{m,n}之后加上一个问号?,就能实现了。 对我有用[0]丢个板砖[0]引用举报管理TOP 精华推荐: 一份笔试题 huntor(逸飞)等 级: #4楼 得分:5回复于:2010-08-24 14:14:35 Java code String find = " \\{.*?\\} " ;