Regex count company hashtags in a tweet (eg. $SPY)

Advertisements

I can count number of hashtags in a tweet with regex (eg. #APPL).

import re
from collections import Counter

def count_tags(tweet):
    pattern = '#[A-Za-z0-9]+'
    return len(re.findall(pattern,tweet))

print(count_tags('#TSLA #APPL #FB up today.'))
>>> 3

But I can’t seem to use same code to count the number of ticker tags (eg. $AAPL)?

import re
from collections import Counter

def count_tags(tweet):
    pattern = '$[A-Za-z0-9]+'
    return len(re.findall(pattern,tweet))

print(count_tags('$TSLA $APPL $FB up today.'))
>>> 0

>Solution :

Since, $ is a regex symbol that means end of string so you need to escape your $ sign:

\$[A-Za-z0-9]+

Demo

Modified source:

import re
from collections import Counter

def count_tags(tweet):
    pattern = '\$[A-Za-z0-9]+'
    return len(re.findall(pattern,tweet))

print(count_tags('$TSLA $APPL $FB up today.'))

Leave a Reply Cancel reply