我试图理解用PHP处理正则表达式.到目前为止,我的代码是:
PHP代码:
$string = "This is a 1 2 3 test.";
$pattern = '/^[a-zA-Z0-9\. ]$/';
$match = preg_match($pattern, $string);
echo "string: " . $string . " regex response: " , $match;
Run Code Online (Sandbox Code Playgroud)
当我认为它应该返回1时,为什么$ match总是返回0?
[a-zA-Z0-9\. ]表示一个字母数字或"."字符.或"".您将需要重复此模式:
$pattern = '/^[a-zA-Z0-9. ]+$/';
^
"one or more"
Run Code Online (Sandbox Code Playgroud)
注意:您不需要.在字符组内转义.