I am using preg_match_all, but I have a problem I am not sure can be solved using this method. The following line is part of what I am retrieving:
XXC033-101-143-147-175-142115-
The sets of numbers (033-101-143, etc) are what I want to refer to. However, the number of sets (always containing three integers) is unknown and can range anywhere from 1 to 10. If I knew there would always only be 2 sets, I would have the following:
if (preg_match_all('#([A-Z]{2}C)([0-9]{3})-([0-9]{3})-([0-9]{6})#', $wwalist, $matches))
...rest of code...
Is there anyway to do this when I have no way of knowing the number of possible sets of 3 integers. They will always be between the #([A-Z]{2}C)
and -([0-9]{6})
.
Any help would be greatly appreciated! Thanks!
Use
'#([A-Z]{2}C)([0-9]{3}-){1,10}([0-9]{6})#'
{1,10}
specifies that the preceding subpattern enclosed in brackets [0-9]{3}-
will repeat 1-10 times.
In addition:
If it can repeat 0 or more times for an indefinite maximum number, use *
.
If it can repeat 1 or more times for an indefinite maximum number, use +
.
Targeting only the 3-digit substrings, individually/optionally capture the groups like this:
Pattern: (Demo)
/[A-Z]{2}C\K(\d{3}-)(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?/