I'm trying to parse strings by a regex in PHP that always have this format:
FooBar(,[0-9]{7}[0-9A-F]{8})+
or in other words, they have a start value/word followed by 1 or multiple entries, each entry is one comma (,
), followed by 7 digits and 8 hexdec characters (digits or uppercase characters A to F).
My Regex to capture this is /^C7(,[0-9]{7}[0-9A-F]{8})+$/
which kind of works. When used in a preg_match_all
it returns an array with two entries, the first as expected the input string, however, in the second array there's only one entry, the last matched chunk. (see Example)
I need to capure all the chunks matched by the capturing group. I did some research and found this answer, which seamed to be about the same issue: https://stackoverflow.com/a/2205009/2989952, So I've adjusted my regex to /(,[0-9]{7}[0-9A-F]{8})+$/
, but I still only get one match. This can be tested at regex101.com. I then experimented some more, and found, that if I change the input string, to contain a space (or any not matched character for that matter), between the chunks, like this: C7,22801422CFE0F63 ,2280141C5EF0F63 ,22801402EFD0F63 ,2280138C5ED0F63 ,228024329897530 ,228023829877530
and adjust the regex once again to /(,[0-9]{7}[0-9A-F]{8})+/
it does exactly as it is intended to do!
Question: Is there a way to achieve this, matching all the chunks in this recurring group without adding whitespaces in between? If so, how?
EDIT
To illustrate the problem:
No Whitespace https://regex101.com/r/ilkZjD/1
Whitespace/random chars https://regex101.com/r/mimBgz/1
Goal: Behaviour of second one, the one with whitespaces, but not adding the whitespaces (respectively the not matched characters).
EDIT 2 (hacky solution)
I kind of found a solution, considering this https://stackoverflow.com/a/3513858/2989952 Answer. The Regex /(?:,)([0-9]{7}[0-9A-F]{8})/
works for me. https://regex101.com/r/LEEFzv/1.However I'd still like a way, to match the initial FooBar
. as that indicates the incoming string should be matched with this regex at all.
(I know I could simply check the string in a second regex for this, I however would love to have it in one regex)
Example:
Input: 'C7,22801422CFE0F63,2280141C5EF0F63,22801402EFD0F63,2280138C5ED0F63,228024329897530,228023829877530'