What's the RegEx pattern to get recursive bracket with specific prefix?

230 Views Asked by At

Please, what is the correct php code, using a regex pattern, to get $output from $string?

$string = 'xmlns(ns=http://testurl.com/now)xpointer(//section/datePublished/text())';

Generalizing:

$string = 'string1(string1_1)string2(string2_1)';

Brackets can be recursive (as text()). i.e. string1_1 and/or string2_1 may contain other brackets. $string = 'string1(string1_1())string2(string2_1()string2_2());

Knowing the brackets prefix xmlns, I need the regex pattern to get //section/datePublished/text(), i.e. knowing string1I need the regex pattern to get string1_1 (or string1_1()).

Knowing the brackets prefix xpointer, I need the regex pattern to get ns=http://testurl.com/now, i.e. knowing string2I need the regex pattern to get string2_1 (or string2_1()string2_2().

Can you explain the process?

1

There are 1 best solutions below

0
mirek On

This can help

preg_match('/xmlns\((.*)\)xpointer\((.*)\)/', $inString, $outArray);
$output = $outArray[2];
$output1 = $outArray[1];

demo: https://www.phpregexonline.com/q/4