Ugh! Not for me!
After finding positive tweets, you want to do it for negative tweets. Your plan now is to find sentences that contain the words hate, dislike or disapprove. You will again save the movie or concert name. You will get the tweet containing the words movie or concert but this time, you don't plan to save the word.
For example, if you have the sentence: I dislike the movie Avengers a lot.
. You match and capture dislike
. You will match but not capture the word movie
. Afterwards, you match and capture anything until the dot.
The list sentiment_analysis
containing the text of three tweets as well as the re
module are loaded in your session. You can use print()
to view the data in the IPython Shell.
This exercise is part of the course
Regular Expressions in Python
Exercise instructions
- Complete the regular expression to capture the words
hate
ordislike
ordisapprove
. Match but don't capture the wordsmovie
orconcert
. Match and capture anything appearing until the.
. - Find all matches of the regex in each element of
sentiment_analysis
. Assign them tonegative_matches
. - Complete the
.format()
method to print out the results contained innegative_matches
for each element insentiment_analysis
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Write a regex that matches sentences with the optional words
regex_negative = r"____.+?____\s____\."
for tweet in sentiment_analysis:
# Find all matches of regex in tweet
negative_matches = re.____(____, ____)
# Complete format to print out the results
print("Negative comments found ____".format(____))