MATLAB: Using regexp to extract labels

regexp

Hi,
I have a file (attached) which includes some header information I am interested in. Specifically, I would like to extract the various channel labels, but can't quite correct the expression in order to obtain all of the label names in the file and nothing else. I have tried
labels = regexp(filetext, '((?<=Label:)(\s*\w*){2}\D*\d*)+', 'match');
although this doesn't quite work due to the first two channel labels being in a different format to the rest. If anyone can offer advice so that I can fix my expression to work for the first two channel labels also, that would be greatly appreciated.
Thank you kindly in advance!

Best Answer

There is no need to be so specific about the format of the data field. It would be enough to identify the 'Label:' and anything that is not a newline, something like this:
>> str = fileread('test.txt');
>> C = regexp(str,'(?<=Label:\s*)[^\n]*','match');
>> C{:}
ans = I
ans = II
ans = A 1-2
ans = A 3-4
ans = A5-6
ans = A 7-8
ans = B 1-2
ans = B 3-4
ans = B 5-6
ans = B 7-8
ans = C 1-2
ans = C 3-4
ans = C 5-6
ans = C 7-8
ans = D 1-2
ans = D 3-4
ans = D 5-6
ans = D 7-8
ans = CS 5-6
ans = E 1-2
ans = E 3-4
ans = E 5-6
ans = E 7-8
ans = F 1-2
ans = F 3-4
ans = F 5-6
ans = F 7-8
ans = G 1-2
ans = G 3-4
ans = G 5-6
ans = G 7-8
ans = H 1-2
ans = H 3-4
... etc
Or if you want to match that format, something like this:
>> C = regexp(str,'(?<=Label: *)[A-Z]+[\-\d ]*','match'); C{:}
ans = I
ans = II
ans = A 1-2
ans = A 3-4
ans = A5-6
ans = A 7-8
ans = B 1-2
ans = B 3-4
ans = B 5-6
ans = B 7-8
ans = C 1-2
ans = C 3-4
ans = C 5-6
...etc
If you want to experiment with regular expressions, then try my FEX submission that lets you quickly change and test regular expressions in an interactive figure: