Java正则表达式在代码中捕获多个组

Java正则表达式在代码中捕获多个组,java,regex,Java,Regex,我需要解析机器生成代码中的多个if和elseif条目,以提取eventName值。我所关心的是我下面字符串中引号中包含的内容的无限量组合 考虑以下代码: String input = "if (eventName== \"event1\") {//blahblah\n}\nelse if (eventName==\"event2\") {//blahblah\n }"; String strPattern = "eventName(?s)==.*\"(.*)\"";

我需要解析机器生成代码中的多个if和elseif条目,以提取eventName值。我所关心的是我下面字符串中引号中包含的内容的无限量组合

考虑以下代码:

  String input = "if (eventName== \"event1\") {//blahblah\n}\nelse if (eventName==\"event2\") {//blahblah\n   }";       
  String strPattern = "eventName(?s)==.*\"(.*)\"";          
  Pattern pattern = Pattern.compile(strPattern,Pattern.CASE_INSENSITIVE);

  Matcher match = pattern.matcher(input);               

  while (match.find()) {
        System.out.printf("group: %s%n", match.group(1));
  }

这只给出了第二个捕获的组事件2。我如何使用eventName==

之间的所有空格和换行符组合来解析上述内容?您可以尝试非贪婪方式

String strPattern = "eventName(?s)==.*?\"(.*?)\"";    

输出:

group: event1
group: event2
第二个正则表达式模式解释:

  eventName==              'eventName=='
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or more times)
  "                        '"'
  (                        group and capture to \1:
    [^"]*                    any character except: '"' (0 or more times)
  )                        end of \1
  "                        '"'
  eventName==              'eventName=='
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or more times)
  "                        '"'
  (                        group and capture to \1:
    [^"]*                    any character except: '"' (0 or more times)
  )                        end of \1
  "                        '"'