Search code examples
phpregexpreg-split

PHP regex preg_split - split by largest group only


I have the following regex

((\$|(\\\[)).*?(\$|(\\\])))

which should capture everything between $$ and \[\] and I tested it on http://gskinner.com/RegExr/ and it's working.

PHP variant is (doubled backslashes)

((\$|(\\\\\[)).*?(\$|(\\\\\])))

and I would like to split my text based on that regex. How can I tell that it uses just the first (and largest group) and not these small ones?

preg_split('/((\$|(\\\\\[)).*?(\$|(\\\\\])))/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

So for text This is my $test$ for something. I should get an array

[0] => This is my 
[1] => $test$
[2] =>  for something.

But I get

[0] => This is my 
[1] => $test$
[2] => $
[3] => 
[4] => $
[5] =>  for something.

Solution

  • You would need something like this:

    $text = 'This is my $test$ for \[something\] new!';
    print_r(preg_split('/(\$.*?\$|\\\\\[.*?\\\\\])/', $text, -1, PREG_SPLIT_DELIM_CAPTURE));
    

    Output:

    Array
    (
        [0] => This is my 
        [1] => $test$
        [2] =>  for 
        [3] => \[something\]
        [4] =>  new!
    )
    

    IMHO, your regex is (probably) wrong. It would fail for texts like Hello $there\]. If you need to capture texts between two $s and a pair of \[ and \], then you need the regexp like:

              <-------------> Match text between \[ and \]
    /(\$.*?\$|\\\\\[.*?\\\\\])/
      <----->   Match text between dollars