Get Started

grepl & grep (2)

You can use the caret, ^, and the dollar sign, $ to match the content located in the start and end of a string, respectively. This could take us one step closer to a correct pattern for matching only the ".edu" email addresses from our list of emails. But there's more that can be added to make the pattern more robust:

  • @, because a valid email must contain an at-sign.
  • .*, which matches any character (.) zero or more times (*). Both the dot and the asterisk are metacharacters. You can use them to match any character between the at-sign and the ".edu" portion of an email address.
  • \\.edu$, to match the ".edu" part of the email at the end of the string. The \\ part escapes the dot: it tells R that you want to use the . as an actual character.

This is a part of the course

“Intermediate R”

View Course

Exercise instructions

  • Use grepl() with the more advanced regular expression to return a logical vector. Simply print the result.
  • Do a similar thing with grep() to create a vector of indices. Store the result in the variable hits.
  • Use emails[hits] again to subset the emails vector.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# The emails vector has already been defined for you
emails <- c("[email protected]", "[email protected]", "[email protected]",
            "invalid.edu", "[email protected]", "[email protected]")

# Use grepl() to match for .edu addresses more robustly


# Use grep() to match for .edu addresses more robustly, save result to hits


# Subset emails using hits

This exercise is part of the course

Intermediate R

BeginnerSkill Level
4.5+
131 reviews

Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.

Mastering R programming is not only about understanding its programming concepts. Having a solid understanding of a wide range of R functions is also important. This chapter introduces you to many useful functions for data structure manipulation, regular expressions, and working with times and dates.

Exercise 1: Useful FunctionsExercise 2: Mathematical utilitiesExercise 3: Find the errorExercise 4: Data UtilitiesExercise 5: Find the error (2)Exercise 6: Beat Gauss using RExercise 7: Regular ExpressionsExercise 8: grepl & grepExercise 9: grepl & grep (2)
Exercise 10: sub & gsubExercise 11: sub & gsub (2)Exercise 12: Times & DatesExercise 13: Right here, right nowExercise 14: Create and format datesExercise 15: Create and format timesExercise 16: Calculations with DatesExercise 17: Calculations with TimesExercise 18: Time is of the essence

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free