Search code examples
phpregexregexp-replace

Remove text outside of [] {} () bracket


I have a string, from which I want to keep text inside a pair of brackets and remove everything outside of the brackets:

Hello [123] {45} world (67)
Hello There (8) [9] {0}

Desired output:
[123] {45} (67) (8) [9] {0}

Code tried but fails:

$re = '/[^()]*+(\((?:[^()]++|(?1))*\))[^()]*+/';
$text = preg_replace($re, '$1', $text);

Solution

  • If the values in the string are always an opening bracket paired up with a closing bracket and no nested parts, you can match all the bracket pairs which you want to keep, and match all other character except the brackets that you want to remove.

    (?:\[[^][]*]|\([^()]*\)|{[^{}]*})(*SKIP)(*F)|[^][(){}]+
    

    Explanation

    Regex demo | Php demo

    Example code

    $re = '/(?:\[[^][]*]|\([^()]*\)|{[^{}]*})(*SKIP)(*F)|[^][(){}]+/m';
    $str = 'Hello [123] {45} world (67)
    Hello There (8) [9] {0}';
    
    $result = preg_replace($re, '', $str);
    
    echo $result;
    

    Output

    [123]{45}(67)(8)[9]{0}
    

    If you want to remove all other values:

    (?:\[[^][]*]|\([^()]*\)|{[^{}]*})(*SKIP)(*F)|.
    

    Regex demo