Reputation: 21198
I want to find all the hashtags in tweets. The code below finds the hashtags, but when printing them out each letter is written out instead of the actual hashtag.
The thing is that I want to create links for the found hashtags which isn't possible now because it should create links for each letter in the hashtags.
What am I doing wrong?
tag_regex = re.compile(r"""
[/^#\S+$/]
""", re.VERBOSE)
for tag in tag_regex.findall(tweet):
print tag
outcome:
#
h
a
s
h
t
a
g
1
#
h
a
s
h
t
a
g
2
Upvotes: 1
Views: 171
Reputation: 336108
The brackets construct a character class which you don't want. Also, you don't want to use regex delimiters /.../
in a language that doesn't use them (a simple string is sufficient, preferably a raw string so you don't need to escape the backslashes). Finally, you shouldn't use anchors if you want to find substrings of the input string:
tag_regex = re.compile(r"#\S+")
Upvotes: 3