我只需要匹配数字和字母,但我不想只匹配数字。
因此,“a1”应该匹配,“1a”也应该匹配。“a”应该匹配。“1”、“1111”、“987”等不应匹配。
<?php
echo preg_match('~^(?![0-9])[0-9a-z]$~', 'a1') . PHP_EOL; // I expect 1
echo preg_match('~^(?![0-9])[0-9a-z]$~', '1a') . PHP_EOL; // I expect 1
echo preg_match('~^(?![0-9])[0-9a-z]$~', '1') . PHP_EOL; // I expect 0
echo preg_match('~^(?![0-9])[0-9a-z]$~', 'a') . PHP_EOL; // I expect 1
我怎样才能达到这些结果?
更新:
它正在工作:
<?php
echo preg_match('~^(?=[a-z]+)|(?![0-9]+)[0-9a-z]+$~', 'a1') . PHP_EOL; // I expect 1
echo preg_match('~^(?=[a-z]+)|(?![0-9]+)[0-9a-z]+$~', '1a') . PHP_EOL; // I expect 1
echo preg_match('~^(?=[a-z]+)|(?![0-9]+)[0-9a-z]+$~', '1') . PHP_EOL; // I expect 0
echo preg_match('~^(?=[a-z]+)|(?![0-9]+)[0-9a-z]+$~', 'a') . PHP_EOL; // I expect 1
echo PHP_EOL;
echo preg_match('~[0-9]*[a-z]+[0-9a-z]*~', 'a1') . PHP_EOL; // I expect 1
echo preg_match('~[0-9]*[a-z]+[0-9a-z]*~', '1a') . PHP_EOL; // I expect 1
echo preg_match('~[0-9]*[a-z]+[0-9a-z]*~', '1') . PHP_EOL; // I expect 0
echo preg_match('~[0-9]*[a-z]+[0-9a-z]*~', 'a') . PHP_EOL; // I expect 1
谢谢你们!