dongyan2445 2010-02-16 20:53
浏览 27
已采纳

为什么我的两个PHP正则表达式都失败了?

$subject = "SPRINT-1.csv";
$pattern = '/^[a-zA-Z]\-[0-9]\.(csv)+$/';
if(preg_match($pattern, $subject)) {
 echo "Match";
} else {
 echo "NOPE";
}

or

$subject = "SPRINT-1.csv";
$pattern = '/^\w\-\.(csv)+$/';
if(preg_match($pattern, $subject)) {
 echo "Match";
} else {
 echo "NOPE";
}
  • 写回答

4条回答 默认 最新

  • 普通网友 2010-02-16 20:55
    关注

    A character class […] does only describe one single character. So [a-zA-Z] describes one character out of az, AZ. The same applies to \w (that’s also a character class).

    You forgot to describe the quantity the characters from that character classes may appear, like:

    • ?: zero or one repetition
    • *: zero or more repetitions
    • +: one or more repetitions
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?