-4

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?

  • Your strategy depends on what you know about the rest of the string. I. e. can you rely on framing portions like `xpointer(` and `xmlns(`? – Carsten Massmann Dec 01 '18 at 08:20
  • You have contradicting pattern in your string that you want to capture. One parenthesis case is, where non-greedy capturing is needed and in other greedy. So a general solution is not possible. We will need more samples to exploit something that can make it work. Like if string starts with alphabet vs string starting with double slash in the parenthesis – Pushpesh Kumar Rajwanshi Dec 01 '18 at 08:47
  • I think this is like your [before question](https://stackoverflow.com/q/53560739/5104748) – Mohammad Dec 01 '18 at 13:13
  • I think it's different, because the intent and the results are different. My first question asks about getting both prefix and the text inside the brackets, without knowing them. My second question asks about getting the text inside the brackets after a known prefix (xpointer or xmlns). – Lorenzo De Tomasi Dec 02 '18 at 20:00
  • Does this answer your question? [Regex: get recursive brackets with prefixes](https://stackoverflow.com/questions/53560739/regex-get-recursive-brackets-with-prefixes) – IS4 Sep 25 '22 at 01:17

1 Answers1

0

This can help

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

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

mirek
  • 71
  • 2
  • 4