I think I have this working, but I'm not sure it's completely accurate.I'm trying to parse Option Symbols. An Option Symbol is made up of 4 parts:Root Symbol (up to 6 characters)Expiration Date (yymmdd)Option Type (1 character)Strike price (8 digits)After parsing the following examples, the results should be as follows:C020216P00035000Root Symbol ='C'Expiration Date = datetime.date(2002, 2, 16)Option Type = PStrike Price = int(00035000) x .001 = 35.00P020216C00040000Root Symbol = 'P'Expiration Date = datetime.date(2002, 2, 16)Option Type = CStrike Price = int(00040000) x .001 = 40.00SBC020216C00030000Root Symbol = 'SBC'Expiration Date = datetime.date(2002, 2, 16)Option Type = CStrike Price = int(00030000) x .001 = 30.00I'm using the following code:import reimport datetime as dtopra_symbol = re.compile(r'(^[^0-9]+)').search(OPRA).group()opra_expiry = dt.datetime.strptime(re.compile(r'\d{2}\d{2}\d{2}').search(OPRA).group(), '%y%m%d').date()opra_cp = re.compile(r'([CP])').search(re.compile(r'([CP]\d+$)').search(OPRA).group()).group()opra_price = int(re.compile(r'(\d+)$').search(OPRA).group()) * .001Is this the best way of getting my results? I'm mostly concerned with the nested regex expression for the Option Type.
Parsing option symbols
python;regex
You say the date, option type and price are all fixed length fields, so you can do:opra_symbol = OPRA[:-15]opra_expiry = dt.datetime.strptime(OPRA[-15:-9]).date()opra_cp = OPRA[-9]opra_price = int(OPRA[-8:]) * .001