How to Parse Nested Shortcode with the Same Name

How to Parse Nested Shortcode with the Same Name

Parsing shortcodes such as this one

[row]
    [col size="6"]...[/col]
    [col size="6"] // second col
        [row]
            [col size="6"]...[/col] // second col end, wrong
            [col size="6"]...[/col]
        [/row]
    [/col]
[/row]

can be tricky because the second [col] is ended by the one nested in the second, nested [row]

so I have found that the best solution is to parse the second [row] first , before the first [row]

just add a the_content filter

add_filter( "the_content", 'parse_content_for_nested_shortcodes' ); 

and this is the function

function parse_content_for_nested_shortcodes($content){
        
        $matches=array();
          $subject = $content;
$pattern = '/[row.*?([row.*?].*?[/row])/';
preg_match_all($pattern, $subject, $matches);
if(count($matches)>0 && isset($matches[1])){
    foreach($matches[1] as $regmatch){
        $content = str_replace($regmatch, stripslashes(do_shortcode($regmatch)), $content);
    }
}


        return $content;
    }