简体   繁体   中英

PHP Regex match one letter and one number

I'm trying to replace any occurrences when you find a single letter followed by a single number in a string.

$word = 'AB001J1'; //or ZR010F2 or ZQ10B5

echo str_replace('/^(?=.*\pL)(?=.*\p{Nd})/', '', $word);

Trying to get the result AB001 //or ZR010 or ZQ10

A regex splitting approach works well here:

$word = 'AB001J1';
$output = preg_split("/(?<=[0-9])(?=[A-Z])/", $word, 2)[0];
echo $output;  // AB001

The above strategy is to split the input string at any point in between a digit and uppercase letter (in that order). This separates the various terms, and we retain only the first one.

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