I am trying to tokenize input below using java regex. I believe that my expression should eagerly match external exec tokens in the program below.
@Test public void test(){ String s = "exec(\n" + " \"command #1\"\n" + " ,\"* * * * *\" //cron string\n" + " ,\"false\" eq exec(\"command #3\")) //condition\n" + ")\n" + "\n" + //split here "exec(\n" + " \"command #2\" \n" + " ,\"exec(\"command #4\") //condition\n" + ");"; List<String> matches = new ArrayList<String>(); Pattern pattern = Pattern.compile("exec\\s*\\(.*\\)"); Matcher matcher = pattern.matcher(s); while (matcher.find()) { matches.add(matcher.group()); } System.out.println(matches); }
I expect a conclusion like
[exec( "command #1" ,"* * * * *" //cron string ,"false" eq exec("command #3")) //condition ),exec( "command #2" ,"exec("command
but get
[exec("command #3")), exec("command #4")]
Can someone help me figure out what I'm wrong about?
source share