Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Regex to find stock tickers (stock symbols)

I am trying to create a regex that finds ticker symbols in bodies of text. However it is a bit of a struggle to get one to do everything I need.

Example:

This is a $test to show what I would LIKE to match. If $YOU look below you will FIND the list of simulated tickers ($STOck symbols) I would like to match.

So in this case I would like to match the following from the above:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • test
  • LIKE
  • YOU
  • FIND
  • STOCK

So as you can see I am trying to get everything after a "$" sign (not including the $) and if it is after the $ then I don’t care about case. Get anything that is in ALL CAPS and between 3-6 characters long. As well as have some room for mistakes $STock where (in this case) only the first two letter after the $ sign are capitals but I would like to match the whole thing before the next space.

I originally had \b[A-Z]{3,6}\b but that matches pretty much every word.

I tried to mix the above with something like: \$[^3-6\s]\S* but that includes the $ and also ignores any ALL CAPS without a dollar sign.

>Solution :

Would you please try the following:

import re

s = 'This is a $test to show what I would LIKE to match. If $YOU look below you will FIND the list of simulated tickers ($STOck symbols) I would like to match.'

print(re.findall(r'(?<=\$)\w+|[A-Z]{3,6}', s))

Output:

['test', 'LIKE', 'YOU', 'FIND', 'STOck']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading