首页 > 解决方案 > regex: select all characters before and after a specific string

问题描述

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

标签: phpregex

解决方案


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


推荐阅读