phpregexsplitpreg-split

Split string on an optional space between a punctuation mark and a letter


I want to split a sentence by some regular expression but I can't get the result exactly as I imagined.

pattern = /[!.\\n]+[^.\d*]/
str = 'immediate! availability. of PHP 5.3.29.a';
preg_split($pattern,$str);

result looks like this

    array (
       0 => 'immediate',
       1 => 'availability',
       2 => 'of PHP 5.3.29',
       3 => '',
    )

but I want the result to look like this..

 array (
     0 => 'immediate!',
     1 => 'availability.',
     2 => 'of PHP 5.3.29.',
     3 => 'a',
 )

How can I achieve this?


Solution

  • Just split according to the space which was preceded by ! or . symbols or a word boundary which was followed by a lowercase letter again followed by a word boundary.

    <?php
    $yourstring = "immediate! availability. of PHP 5.3.29.a";
    $regex = '~(?<=[!.]) |\b(?=[a-z]\b)~';
    $splits = preg_split($regex, $yourstring);
    print_r($splits);
    ?>
    

    Output:

    Array
    (
        [0] => immediate!
        [1] => availability.
        [2] => of PHP 5.3.29.
        [3] => a
    )