Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for stock tickers - Python

Tags:

python

regex

I have a list of tweets. They look like this:

data = [['trading $aa $BB stock market info'],
        ['$aa is $116 market is doing well $cc $ABC']]

I want to extract stock tickers:

['$aa', '$BB']
['$aa', '$cc', '$ABC']]

I have tried this:

for i in data:
    print re.findall(r'[$]\S*', str(i))

And, the output contains $116 as well:

['$aa', '$BB']
['$aa', '$116', '$cc', '$ABC']]

Any suggestions?

like image 401
kevin Avatar asked Jun 24 '26 14:06

kevin


2 Answers

Match the dollar sign, one letter, and then anything that's not a space:

re.findall(r'[$][A-Za-z][\S]*', str(i))
like image 120
Harald Nordgren Avatar answered Jun 26 '26 03:06

Harald Nordgren


I'll just leave this here for people looking for a regex that matches a stock ticker

re.fullmatch('([A-Za-z]{1,5})(-[A-Za-z]{1,2})?', symbol)
like image 42
Tom Sawyer Avatar answered Jun 26 '26 04:06

Tom Sawyer



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!