Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matching {{string}} in regex c#

Tags:

c#

regex

what is the regex for matching a string within two curly brackets as in

{{string}}

result should be string.

Do I have to escape both curly brackets?

like image 252
Alex J Avatar asked Oct 19 '11 17:10

Alex J


1 Answers

No, actually the following should work just fine:

"{{([^}]*)}}"

Edit: As pointed out by dtb the expression above fails for a string containing a single } within the double brackets. To handle this case the following example would do a much better job:

"{{((?:}(?!})|[^}])*)}}"

Edit 2: The simplest solution however would probably be the following:

"{{(.*?)}}"
like image 59
DeCaf Avatar answered Sep 21 '22 12:09

DeCaf