2

The title is probably confusing, but I have no idea how to properly phrase this.

So here's my goal. I have this string (or something like it):

[some_element]Random string chars [some_element]Ramdon[/some_element] some more random chars[/some_element]

(Some of you may recognize that these are WordPress shortcodes, but this methodology would still be useful elsewhere to me as well.)

What I need to do is match the parent "element". My usual approach might be something like this:

\[(\w+)].*?\[\/\1]

The problem is, this won't work in the above example, because it's "child element" has a the same closing "tag".

How could I get this regex to work, reglardless of how many nested children exist (literally, an infinite number of duplicate nested children)?

6
  • 1
    This is a great example of when regex is not the right tool for the job.
    – Biffen
    Commented Nov 24, 2014 at 20:19
  • What language/tool are you using for regex?
    – anubhava
    Commented Nov 24, 2014 at 20:20
  • @Biffen ...okay, then what is the right tool? Commented Nov 24, 2014 at 20:22
  • @anubhava PHP is the language Commented Nov 24, 2014 at 20:22
  • @Biffen then do something productive and tell the OP what is the right tool.
    – ddavison
    Commented Nov 24, 2014 at 20:24

2 Answers 2

1

You can use this recursive regex in PHP:

$re = '~\s* ( \[some_element\] ( (?: .* | (?1) )* ) \[/some_element\] )~x';

RegEx Demo

This will give you this string in matched group #2:

Random string chars [some_element]Ramdon[/some_element] some more random chars
1
  • Whoah, this seems like it will work. I don't know a ton about regex, and I haven't heard of this before, but I think it's exactly what I was looking for. I'll learn more about this, but I think you got it. Thanks! Commented Nov 25, 2014 at 15:05
1

This looks like a job for recursive patterns (in php).
But I am sadly way too inexperienced to write the pattern here without trying :(
Maybe you can figure that out yourself. I am going to try it too, but that's gonna take a while...

would you look at that:

(The words between {[< and >]} are not part of the pattern, they describe what the subpattern should do.)

[ ( ( {[< some way to match any string except [word] >]} ) | (?R) )* ]

1
  • Your answer is definitely what I'm looking for, just not an exact answer to my question like @anubhava's was. Thank you though! Upvote forsure. Commented Nov 25, 2014 at 15:06

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.