2

I'm trying to write a simple regex to match nested curly brackets. So if I have this text:

{
  apple
  {second}
  banana
}

Then I want it to match the entire text between the first and last {} (including the 2nd pair of {}). Here's the regex I've written:

/{ (?:.+?|(?R) ) }/six

The output for this is:

{ apple {second} 

As you can see the first curly bracket is being matched, and the 'banana' at the end is not being matched. Here's the output I want it to return:

apple {second} banana 

What am I doing wrong?

4
  • Are you trying to match the entire inside, or do you want to match the inside as 3 separate tokens? Commented Nov 18, 2012 at 2:08
  • @Corbin The entire inside as one string/token. Commented Nov 18, 2012 at 2:08
  • Seems like you don't need regex then? Just strip off the first/last characters. Or am I missing something? If you needed to make sure the braces were there, you could check the first and last characters explicitly, or use a simple pattern like {(.+)} Commented Nov 18, 2012 at 2:12
  • @Corbin Yes, you are missing something. The string I've posted is just a part of a larger text, so I need to use regex to parse it out. Commented Nov 18, 2012 at 2:12

2 Answers 2

2

The pattern you want to use is:

/{ (?:  (?R) | .+? )+ }/six

With your regex, the .+? would have always taken precedence. PCRE would match the longest possible string and never look for the alternative.

Only making the alternative (..)+ repetetive allows the matching to switch between the recursive part and the match-anything placeholder.

Sign up to request clarification or add additional context in comments.

8 Comments

Have you tried running this regex? When I run it as is, my apache always crashes.
What's the + after the last ) for? It seems to be the culprit and crashes apache, when I remove it then the regex doesn't capture correctly{}, but doesn't crash.
Backtracking limit. Just tested on CLI. You might try to use [^{}]+ in place of the .+? though.
That worked. But its matching the full text instead of just the stuff inside the first set of brackets. Output: { apple {second} banana }
That's what it is supposed to do. It always returns the complete match; only asserts the structure within, no cutting out.
|
0

Does this work for you?

\{([\s\S]*)\}

1 Comment

Yes, but I prefer to do it via (?R) as I'm trying to learn how that works too. Can you tell me what's wrong with my earlier regex?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.