Regular expressions can be challenging to use when you're not familiar with them.
Here's one way to find a hashtag:
tweet = 'it is fun to post on #stackoverflow, really';
regexp(tweet,'#(\w+)','tokens','once')
ans =
'stackoverflow'
What does #(\w+)
do? We look "word"-characters (no spaces, no punctuation) with \w
, and specify that we want at least one of them +
. The word has to start with a #
. We use the parentheses to indicate which part we want to be returned, and the option tokens
so that it returns the match (I don't care where the hashtag is in the tweet). I set the option once
so that regexp
only looks for one hashtag; don't do that if you expect multiple hashtags in your tweet, though note that the output will be a cell array of strings.