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?
Aregmatches()
Bgrepl()
Cgsub()
Dgregexpr()
✗ Incorrect
gregexpr() returns the starting positions of all matches of a pattern in a string.
What does the regex symbol \d represent in R?
AAny letter
BAny whitespace
CAny digit (0-9)
DStart of string
✗ Incorrect
\d matches any digit from 0 to 9.
Which function would you use to replace text matching a regex pattern in R?
Agsub()
Bgregexpr()
Cgrepl()
Dregexpr()
✗ Incorrect
gsub() replaces all occurrences of a pattern with a replacement string.
What does the regex pattern abc$ mean?
AString ends with 'abc'
BString starts with 'abc'
CString contains 'abc' anywhere
DString has 'abc' repeated
✗ 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?
Agsub()
Bgrepl()
Cregmatches()
Dgregexpr()
✗ 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.
Practice
(1/5)
1. What does the grepl() function do in R when used with regular expressions?
easy
A. It checks if a pattern exists in a string and returns TRUE or FALSE.
B. It replaces all occurrences of a pattern in a string.
C. It extracts all matches of a pattern from a string.
D. It splits a string into parts based on a pattern.
Solution
Step 1: Understand the purpose of grepl()
grepl() returns a logical value indicating if the pattern is found in the string.
Step 2: Compare with other functions
Functions like gsub() replace text, and strsplit() splits strings, so they do not match grepl()'s behavior.
Final Answer:
It checks if a pattern exists in a string and returns TRUE or FALSE. -> Option A
Quick Check:
grepl() = logical check [OK]
Hint: Remember: grepl() returns TRUE/FALSE if pattern found [OK]
Common Mistakes:
Confusing grepl() with gsub() which replaces text
Thinking grepl() extracts matched text
Assuming grepl() splits strings
2. Which of the following is the correct syntax to replace all occurrences of "cat" with "dog" in the string "cat and cat" using regular expressions in R?
easy
A. sub("cat", "dog", "cat and cat")
B. str_replace_all("cat and cat", "cat", "dog")
C. gsub("cat", "dog", "cat and cat")
D. replace("cat", "dog", "cat and cat")
Solution
Step 1: Identify function for replacing all occurrences
gsub() replaces all matches, while sub() replaces only the first.
Step 2: Check syntax correctness
gsub(pattern, replacement, string) is the correct order and usage in R.
Final Answer:
gsub("cat", "dog", "cat and cat") -> Option C
Quick Check:
Replace all = gsub() [OK]
Hint: Use gsub() to replace all matches, sub() only first [OK]
Which regular expression pattern would correctly identify emails ending with ".com" using grepl()?
hard
A. "\\.com$"
B. ".com"
C. "com$"
D. "\\.com"
Solution
Step 1: Understand the pattern requirements
We want to match emails ending exactly with ".com". The dot must be escaped as "\\." to match a literal dot, and "$" means end of string.
Step 2: Analyze each option
"\\.com$" uses "\\.com$" which matches '.com' at the end. ".com" matches any character before 'com', not necessarily end. "com$" misses the dot. "\\.com" matches '.com' anywhere, not necessarily at end.
Final Answer:
"\\.com$" -> Option A
Quick Check:
Escape dot and use $ for end match [OK]
Hint: Escape dot with \\. and use $ to match string end [OK]
Common Mistakes:
Not escaping dot, so matches any character
Missing $ causing partial matches
Confusing string end anchor with start anchor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.