Java正则表达式模式问题

Java正则表达式模式问题,java,regex,Java,Regex,我有一个字符串: bundle://24.0:0/com/keop/temp/Activator.class 从这个字符串中,我需要得到com/keop/temp/Activator,但是以下模式: Pattern p = Pattern.compile("bundle://.*/(.*)\\.class"); 仅返回Activator。我的错误在哪里?您的正则表达式使用贪婪匹配与匹配任何字符(但换行符除外)的*/读取最后一个/,(.*)\\.匹配最后一个时段的所有内容。您可以将匹配的字符

我有一个字符串:

bundle://24.0:0/com/keop/temp/Activator.class
从这个字符串中,我需要得到
com/keop/temp/Activator
,但是以下模式:

Pattern p = Pattern.compile("bundle://.*/(.*)\\.class"); 

仅返回
Activator
。我的错误在哪里?

您的正则表达式使用贪婪匹配与匹配任何字符(但换行符除外)的
*/
读取最后一个
/
(.*)\\.
匹配最后一个时段的所有内容。您可以将匹配的字符限制在要匹配的字符串之前,而不是延迟匹配。改为

Pattern p = Pattern.compile("bundle://[^/]*/(.*)\\.class"); 
示例代码:

String str = "bundle://24.0:0/com/keop/temp/Activator.class";
Pattern ptrn = Pattern.compile("bundle://[^/]*/(.*)\\.class");
Matcher matcher = ptrn.matcher(str);
if (matcher.find()) {
   System.out.println(matcher.group(1));
报告的产出:


您需要使用
跟随初始标记
*
,以进行匹配

com/keop/temp/Activator
bundle://.*?/(.*)\\.class
           ^