I'm currently having a small problem with the following regex in PHP:
preg_match_all('/(.*)(XS|S|M|XL|XXL|L)(.*)/i', '00236XL00', $arr, PREG_PATTERN_ORDER);
It returns L
for the size when I want XL
in this case.
It works fine if I supply an ID with just L
(e.g. 002465L00) in it.
Can you tell me what I did wrong?
I tried moving the L
to the end of the group but it did not help.
The first group, (.*)
, is greedy, and will match as much as it can. Since the second group will still match if the first one "takes" 00236X
, that is what will happen. The simplest fix, if the data presented above is typical, is to change the .
to [0-9]
. Will this still match your data? If not, please give more examples.
To avoid the greedyness, you can make the other groups lazy instead, by using the ?
operator. So, something like /(.*?)(XS|S|M|XL|XXL|L)(.*)/i
should work.
Seems that your ID's are only digits surrounding the size code.
So what about to look for non-digit
characters?
preg_match_all('/([^0-9]+)/i', '00236XL00', $arr, PREG_PATTERN_ORDER);
EDIT after author's comment:
preg_match_all('/[A-Z][A-Z]\-\d+([A-Z]+)\d+/i', 'XX-00236XL00', $arr, PREG_PATTERN_ORDER);