I'm trying to get the a regex running for the following type of strings: one upper case letter followed by a numeric value. The string can consist of multiple of these letter-numeric-value combinations. Here some examples and my expected output:
A12B8Y9CC10
-> output [0 => 12, 1 => 8, 2 => 9] (10 is ignored, because there are two letters)
V5C8I17
-> output [0 => 5, 1 => 8, 2 => 17]
KK18II9
-> output [] (because KK and II are always two letters followed by numeric values)
I8VV22ZZ4S9U2
-> output [0 => 8, 1 => 9, 2 => 2] (VV and ZZ are ignored)
A18Z12I
-> output [0 => 18, 1 => 12] (I is ignored, because no numeric value follows)
I tried to reach this by the following regex using preg_match: /^([A-Z]{1}\d{1,)$/
But it doesn't give the expected output. Can you please help me, how to solve this?
Thanks and best regards!
You may use this regex in php
using preg_match_all
:
preg_match_all('/(?<![a-zA-Z])[a-zA-Z]\K\d+/', $string, $matches);
Resulting in array $matches[0]
to return all the matches.
RegEx Details:
(?<![a-zA-Z])
: Make sure we don't have a letter before current position[a-zA-Z]
: Match a letter\K
: Reset match info\d+
: Match 1+ digits