1

OK, so, let's say I have some text like :

Any random content here
{{mycontent
more content goes here
{{curly braces may also appear in here}}
{even single ones}

}}

And more content goes here...

I need to fetch the part between {{mycontent and its corresponding }} closing brackets.

How would you go about that, in RegEx (that's a must) ?

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • What language are you using ? Also what have you tried ? – HamZa Jan 26 '14 at 10:26
  • @HamZa Well, I'm using PHP, but that's not the point. Btw, I'm already able to achieve what I'm asking in the first place; but in PHP, not in RegEx. Nested patterns always troubled me in Regular Expressions. (I know it may not be tha best approach to the problem, but it *has* to be...) – Dr.Kameleon Jan 26 '14 at 10:31
  • Well since you have committed to the community, I'll help you with this one ... [Have fun](http://regex101.com/r/qK6mA7) – HamZa Jan 26 '14 at 10:34
  • Ah, also the language does matter. Most regex flavors can't do what you want. The above pattern should work in PCRE and Perl. If you want a .NET solution, you might edit [this answer](http://stackoverflow.com/a/19694656) to your needs. The first pattern I gave is a slight change of [this answer](http://stackoverflow.com/a/14952740). – HamZa Jan 26 '14 at 10:37
  • @HamZa Let me go through your suggestions and I'll let you know. Unfortunately, my main project is in PHP/CodeIgniter, so using Perl (although possible) would not be preferable... Thanks a lot! – Dr.Kameleon Jan 26 '14 at 10:39
  • PS: PHP uses PCRE. So the first pattern should work. Take care – HamZa Jan 26 '14 at 10:40

1 Answers1

-2

/\{{2}(.*)\}{2}/s

This will match anything between 2 curly braces including new lines.

The result would be

0 => string '{{mycontent
more content goes here
{{curly braces may also appear in here}}
{even single ones}

}}'
Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
  • Well, you're just relying on greediness. Something like this would fail `{{ help }} {{ you }}` – HamZa Jan 26 '14 at 10:28