1

I am trying to match a nested content section of the config based on the alias.

I have the base prototype working for matching single aliases over here: https://regex101.com/r/1vwKsx/1 (section correctly matched to: 'template_path_stack' =>)

HOWEVER, I want to select a section (which is re-used in the file) based on the section container. In the link above, I need to only match the section which is inside: controllers => factories.

The problem is that the regex matches both (the correct one and the one from the outside). https://regex101.com/r/IrV0SN/1

Current regex:

('factories' => )(\[((?>[^\[\]]++|(?2))*)\])
2
  • 1
    Like regex101.com/r/elP51F/1? Commented Nov 28, 2021 at 22:57
  • @WiktorStribiżew Damn, you are good. That is perfect thanks! Add an answer and I will mark it correctly. Commented Nov 28, 2021 at 22:58

1 Answer 1

3

You may add an obligatory prefix to the regex and discard the matched text using \K operator:

'controllers' => \[\s*\K('factories' => )(\[((?>[^][]++|(?2))*)])

See the regex demo.

Here,

  • 'controllers' => \[ - a 'controllers' => [ text
  • \s* - zero or more whitespaces
  • \K - discards the text matched so far from the current overall match memory buffer.
Sign up to request clarification or add additional context in comments.

Comments

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.