我有一個正常工作的正則表達式代碼,但它不包括非字母字符。我將如何包括這些?在正則表達式搜索中包含無字母字符
$text = "i have one treehouse. i'm the one. I have two cats.";
preg_match_all('/[\w\s]+?\bone\s?[\w\s]*?\./', $text, $array);
print_r($array);
預期結果
$array[0] = "i have one treehouse.";
$array[1] = "i'm the one";
實際結果
$array[0] = "i have one treehouse.";
$array[1] = "m the one"; <---cuts off at the single quote
我想這是因爲正則表達式的代碼不查找非字母字符,例如」!?等等。我如何包括這些?
您使用的字符組目前:'[\ W \ S]'。爲此,請添加您想包含的特殊字符:'[\ w \ s',!]' – OnlineCop
等待。我以前看過類似的帖子,我認爲 – user5173426
'[]'是一個字符類,列出你想要的字符。把你允許的角色放在裏面。 – chris85