正则表达式按空格和所有标点符号拆分单词,除了

Regex split words by spaces and all punctuation marks except

我正在尝试将文件拆分成单词,以任何类型和任意数量的空格和标点符号分隔,但以下标点符号“-”除外。我该怎么做?这是我目前拥有的,但它没有按期分开。

$words = preg_split("/((?![a-zA-Z'-’])\s)+/",$file);

使用preg_match_all更简单:

preg_match_all("~[A-Z'’-]+~ui", $str, $m);
$words = $m[0];

我添加了 u 修饰符,因为 超出了 ascii 范围。 如果您需要除 ascii 字母、引号或连字符以外的其他字符,请将它们添加到字符 class.