Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I breakdown a string in multiple components

Tags:

python

string

I have a string 'ABCAPITAL23JAN140CE'. This is the symbol for an option traded on stock exchange. ABCAPITAL part of the string is the company name. 23 is year 2023. JAN is for month. 140 is the strike price and CE is the type of the option.

All these components can vary for different options.

I need a function such that pieces_of_string = splitstring('ABCAPITAL23JAN140CE')

where pieces_of_string = ['ABCAPITAL', 23, 'JAN', 140, 'CE'] is returned

how do I do that?

like image 568
KawaiKx Avatar asked Jun 03 '26 14:06

KawaiKx


1 Answers

You might use re.findall with [A-Z]+|\d+

See the matches here on regex101

import re
print(re.findall(r"[A-Z]+|\d+", "ABCAPITAL23JAN140CE"))

# Or converting to int
print([int(v) if v.isdigit() else v for v in re.findall(r"[A-Z]+|\d+", "ABCAPITAL23JAN140CE")])

Output

['ABCAPITAL', '23', 'JAN', '140', 'CE']
['ABCAPITAL', 23, 'JAN', 140, 'CE']

Another option with 4 capture groups matching the digits and the shorted part for the month like JAN FEB etc...

^(\S*?)(\d+)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\d+)(\S+)$

See the capture group matches on regex101

import re
m = re.match(r"(\S*?)(\d+)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\d+)(\S+)$", "ABCAPITAL23JAN140CE")
if m:
    print(list(m.groups()))

Output

['ABCAPITAL', '23', '140', 'CE']
like image 96
The fourth bird Avatar answered Jun 05 '26 04:06

The fourth bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!