RegEX - Check if string starts with certain text, then look for groups

Hi there,

I am learning RegEX right now for a parser that is parsing responses from a TCP/IP Device. So far this is going really well, however I am stuck on this one:

The message sent by the device looks like this:

ALL 02 02 02 05 05 05 08 08 08 08 08 08 08 08 08 08

I would like to check if the string starts with the word ALL; after I would like to get each 2 digits either as a group, or as separate matches. The number of 2 digit numbers may vary.

So for example my result would look like

Group 1 ALL
Group 2 02
Group 3 02
Group 4 02

and so on…

Any hints?

Thanks a lot in advance,


You have to know how many groups to expect. Do you? If not, this has to be done with two separate matches patterns.

If you know there are 16 groups (as in your example), this is the pattern:

^ALL ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2}) ([a-f0-9]{2})$

If you don’t, then the first pattern would be:

^ALL( [a-f0-9]{2})+

Then repeatedly run this pattern on group 1:


Hi Kem,

the number to expect differs by device type, it can be anything from 8 to 16. So by repeatedly running the pattern on the other groups you mean do one regex, and then do a seperate regex on the result of the first reg ex?

I hoped I can avoid that in one query :slight_smile:



Yes, that’s what I meant, run the second pattern on the result of the first.

If you know there will never be more than 16, you could do something like this too.

(For brevity, this pattern assumes there can be 2 to 4, just to give you an idea.)

^ALL( [0-9a-f]{2})( [0-9a-f]{2})( [0-9a-f]{2})?( [0-9a-f]{2})?

You’d trim each subgroup, and SubExpressionCount would tell you how many there are.

Yes, I know there won’t be more than 16. I’m doing an OS X software for a product that has a rather shitty webinterface, a Windows software and is not able to backup settings - so I’m adding that to my little tool.

Thank you, this is going to help big time!


To offer an alternative to RegEx - you can also use SplitB and an Array:

Dim theGroups(-1) As String If LeftB(theMessage, 3) = "ALL" Then theGroups = SplitB(theMessage, " ") End If
You can then use theGroups.Ubound + 1 to get the number of groups. You can then use the array methods to further deal with the resulting members.

This is one case where I would pick the non-RegEx approach. The code is simpler and likely faster.

If there are no other types of ALL lines, I agree.