简体   繁体   中英

php regex to match a single letter after digits

I've got the following regex to see if $string contains digits followed by letters. However, I need it to check only that it contains 1 letter after the numeric value. 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 = "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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