1

I want to select all text before and after a specific substring, I used the following expression to do that, but it not selecting all the needed text:

/^(?:(?!\<\?php echo[\s?](.*?)\;[\s?]\?\>).)*/

for example:

$re = '/^(?:(?!\<\?php echo[\s?](.*?)\;[\s?]\?\>).)*/';
$str = 'customFields[<?php echo $field["id"]; ?>][type]';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

it will select only this part customFields[, while the expected result should be customFields[ and ][type]

check this link for debugging

Mohammad
  • 3,449
  • 6
  • 48
  • 75

2 Answers2

1

The pattern ^(?:(?!\<\?php echo[\s?](.*?)\;[\s?]\?\>).)* uses a tempered greedy token which matches any character except a newline from the start of the string ^ that fulfills the assertion of the negative lookahead.

That will only match customFields[

For your example data you could make use of a tempered greedy token regex demo, but instead you could also just make use of a negated character class and SKIP FAIL:

^[^[]+\[|<\?php echo\s(.*?)\;\s\?\>(*SKIP)(*FAIL)|\]\[[^]]*\]

Regex demo | Php demo

For example

$re = '/^[^[]+\[|<\?php echo\s(.*?)\;\s\?\>(*SKIP)(*FAIL)|\]\[[^]]*\]/';
$str = 'customFields[<?php echo $field["id"]; ?>][type]';
preg_match_all($re, $str, $matches, PREG_SET_ORDER);
print_r($matches);

Result

Array
(
    [0] => Array
        (
            [0] => customFields[
        )

    [1] => Array
        (
            [0] => ][type]
        )

)

To get a more exact match you might also use capturing groups:

^((?:(?!<\?php echo[\s?](?:.*?)\;\s\?>).)*)<\?php echo\s(?:.*?)\;[\s?]\?>(.*)$

regex demo | Php demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

What about using positive lookarounds:

(.*)(?=\<\?php echo)|(?<=\?\>)(.*)

Demo

ALFA
  • 1,726
  • 1
  • 10
  • 19