thisextendsthat
thisextendsthat

Reputation: 1345

Regex - find text surrounded by double curly brackets ( `{{text}}` ) but not triple brackets ( '{{{text}}}' )

I am parsing email content for Handlebars.Net. Use case is like so:

string template = "{{I should be found}} but {{{I shouldn't}}}";

I think I need to use some combination of lookaheads and lookbehinds to find these tags but I cannot get this to work. I've created a regex which is close to what I need but is not matching exactly like how I need to - [^{]({{.*}})(!?})

Any ideas?

Upvotes: 3

Views: 191

Answers (1)

anubhava
anubhava

Reputation: 785541

You can use this regex with a negative lookaround assertions:

(?<!{){{[^{}]+}}(?!})

RegEx Demo

  • (?!}) is a negative lookahead assertion that fails the match if next character is }
  • (?<!{) is a negative lookbehind assertion that fails the match if previous character is {

Upvotes: 4

Related Questions