Recall & Review
beginner
What is a regular expression (regex) in R?
A regular expression is a pattern of characters used to find or match text in strings. In R, regex helps search, replace, or split text based on these patterns.
Click to reveal answer
beginner
Which R function is commonly used to check if a pattern exists in a string?
The
grepl() function returns TRUE if the pattern is found in the string, otherwise FALSE.Click to reveal answer
intermediate
How do you extract matched text using regex in R?
Use
regmatches() with gregexpr() to extract all matches of a pattern from a string.Click to reveal answer
beginner
What does the regex pattern
^abc mean?It means the string must start with the characters 'abc'. The caret
^ anchors the match to the start of the string.Click to reveal answer
intermediate
How can you replace all digits in a string with '#' in R?
Use
gsub("\\\d", "#", text). Here, \\\d matches any digit, and gsub() replaces all matches.Click to reveal answer
Which function returns the positions of regex matches in a string in R?
✗ Incorrect
gregexpr() returns the starting positions of all matches of a pattern in a string.
What does the regex symbol
\d represent in R?✗ Incorrect
\d matches any digit from 0 to 9.
Which function would you use to replace text matching a regex pattern in R?
✗ Incorrect
gsub() replaces all occurrences of a pattern with a replacement string.
What does the regex pattern
abc$ mean?✗ Incorrect
The dollar sign $ anchors the match to the end of the string.
Which function returns TRUE or FALSE if a pattern is found in a string?
✗ Incorrect
grepl() returns a logical value indicating if the pattern exists.
Explain how to find and extract all occurrences of a pattern in a string using R regex functions.
Think about functions that locate positions and then extract text.
You got /4 concepts.
Describe how to replace all digits in a string with a specific character using regular expressions in R.
Consider the function that replaces all matches, not just the first.
You got /4 concepts.