Search code examples
regexmatlabbloomberg

Extract root, month letter-year and yellow key from a Bloomberg futures ticker


A Bloomberg futures ticker usually looks like:

MCDZ3 Curcny

where the root is MCD, the month letter and year is Z3 and the 'yellow key' is Curcny.

Note that the root can be of variable length, 2-4 letters or 1 letter and 1 whitespace (e.g. S H4 Comdty). The letter-year allows only the letter listed below in expr and can have two digit years. Finally the yellow key can be one of several security type strings but I am interested in (Curncy|Equity|Index|Comdty) only.

In Matlab I have the following regular expression

expr = '[FGHJKMNQUVXZ]\d{1,2} '; 
[rootyk, monthyear] = regexpi(bbergtickers, expr,'split','match','once');

where

rootyk{:}
ans = 
    'mcd'    'curncy'

and

monthyear = 
    'z3 '

I don't want to match the ' ' (space) in the monthyear. How can I do?


Solution

  • Assuming there are no leading or trailing whitespaces and only upcase letters in the root, this should work:

    ^([A-Z]{2,4}|[A-Z]\s)([FGHJKMNQUVXZ]\d{1,2}) (Curncy|Equity|Index|Comdty)$
    

    You've got root in the first group, letter-year in the second, yellow key in the third.

    I don't know Matlab nor whether it covers Perl Compatible Regex. If it fails, try e.g. with instead of \s. Also, drop the ^...$ if you'd like to extract from a bigger source text.