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.
The greedy
.*matches anything (except line breaks), so when there are more than one}}in the string, it always matches the last}}(if there aren’t any\rand\nbetween the two}}!).Try to make the
.*match reluctant (ungreedy) like this:That’s correct, you needn’t escape the
}.You could also do:
if a
{{ ... }}cannot contain a single}itself.