Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regular expression to match {{characters inside double curly brace}}

Tags:

java

regex

I am trying to match everything inside double curly brackets in a string. I am using the following expression:

\{\{.*\}\}

Some examples:

The {{dog}} is not a cat. This correctly matches {{dog}}

However,
The {{dog}} is a {{cat}} matches everything after the first match instead of returning two matches. I want it to match twice, once for {{dog}} and once for {{cat}}

Does anyone know how to do this?

Thanks.

like image 697
toc777 Avatar asked Jul 18 '11 14:07

toc777


1 Answers

You have to use non-greedy match:

\{\{.*?\}\}

to match everything between braces, use:

\{\{(.*?)\}\}
like image 139
hsz Avatar answered Oct 14 '22 01:10

hsz