正则表达式匹配标点符号和字母数字字符

Pau*_*ner 1 php regex preg-match

我试图测试一个字符串,看它是否包含字母数字或标点符号以外的字符,如果是,则设置错误.我有下面的代码,但它似乎没有工作,因为它让"CZW205é"通过.我对正则表达式毫无希望,似乎无法解决问题.

if(!preg_match("/^[a-zA-Z0-9\s\p{P}]/", $product_id)) {
    $errors[$i] = 'Please only enter alpha-numeric characters, dashes, underscores, colons, full-stops, apostrophes, brackets, commas and forward slashes';
    continue;
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

ste*_*ema 8

你可以做

if(preg_match("/[^a-zA-Z0-9\s\p{P}]/", $product_id)) {
    $errors[$i] = 'Please only enter alpha-numeric characters, dashes, underscores, colons, full-stops, apostrophes, brackets, commas and forward slashes';
    continue;
}
Run Code Online (Sandbox Code Playgroud)

[^...] 是一个否定的字符类,它会在找到不在你的类中的东西时立即匹配.

(为此我删除了之前的否定preg_match())