使用正则表达式拆分字符串


Splitting a string using regex

我想拆分一个字符串,其中任何字符都是空格或标点符号(不包括撇号)。以下正则表达式按预期工作。

/[^a-z']/i

像"我会"和"没有"这样的词被接受,这很好。

问题在于像'ere和'im这样的词。我想删除开头的撇号,并带有im和ere这两个词。

如果可能的话,理想情况下,我想在正则表达式模式中停止/删除它。

提前谢谢。

试试这个

$str = "Words like I'll and Didn't are accepted, which is great. 
        The problem is with words like 'ere and 'im";
print_r(preg_split("/'?[^a-z']+'?/i", $str));
//Array ( [0] => Words [1] => like [2] => I'll [3] => and [4] => Didn't ... 
//        [16] => ere [17] => and [18] => im )