Regular expression greed (java)

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 #4") //condition );] 

but get

 [exec("command #3")), exec("command #4")] 

Can someone help me figure out what I'm wrong about?

+4
source share
1 answer

The default is a dot character . does not match newlines. Here, in this case, the "exec" pattern will only match if it appears on the same line.

You can use Pattern.DOTALL so that you can match with newlines:

 Pattern.compile("exec\\s*\\(.*\\)", Pattern.DOTALL); 

Alternatively, you can specify (?s) , which is equivalent to:

 Pattern.compile("(?s)exec\\s*\\(.*\\)"); 
+3
source

All Articles