简体   繁体   English

php正则表达式匹配数字后的单个字母

[英]php regex to match a single letter after digits

I've got the following regex to see if $string contains digits followed by letters. 我有以下正则表达式,看看$ string是否包含数字后跟字母。 However, I need it to check only that it contains 1 letter after the numeric value. 但是,我需要它只检查它在数值后面包含1个字母。 Although the code below works, if $string was to be 28AB then it will also match the regex but it shouldn't, it should only match any numeric value followed by a single letter. 虽然下面的代码有效,但如果$ string是28AB,那么它也会匹配正则表达式,但不应该,它应该只匹配任何数字后跟一个字母。

$string = "28A";

$containsSpecial = preg_match('/[d\^a-zA-Z]/', $string);
^\d+[a-zA-Z]$

Try this.Your code uses [] which can match any character out of the list provided.Also use anchors to make a strict match and no partial matches. 试试这个。你的代码使用[] ,可以匹配提供的列表中的任何字符。也可以使用锚点进行严格匹配,不进行部分匹配。 See here 看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM