正则表达式 - php preg_split结果

时间:2014-08-20 06:04:49

标签: php regex preg-match

我想通过一些正则表达式来分割句子 但我无法完全按照我的想象得到结果。

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

结果看起来像这样

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

但我希望结果看起来像这样..

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

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

根据前面有!.符号或单词边界的空格进行拆分,后跟一个小写字母后跟一个单词边界。

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

<强>输出:

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

答案 1 :(得分:0)

您也可以尝试使用Lookaround

(?<=\.|!) *(?=\D)

Online demo

模式说明:

  (?<=                     look behind to see if there is:
    \.                       '.'
   |                        OR
    !                        '!'
  )                        end of look-behind
   *                       ' ' (0 or more times)
  (?=                      look ahead to see if there is:
    \D                       non-digits (all but 0-9)
  )                        end of look-ahead