pattern is the regular expression you want to use.
text is the string or vector of strings to search.
Examples
Checks if "cat" is in the text. Returns TRUE or FALSE.
R Programming
grepl("cat", "The cat is here")
Replaces the first "cat" with "dog".
R Programming
sub("cat", "dog", "The cat is here")
Replaces all spaces with dashes.
R Programming
gsub(" ", "-", "Hello world")
Extracts the first number sequence from the text.
R Programming
regmatches("abc123", regexpr("[0-9]+", "abc123"))
Sample Program
This program checks which words contain numbers, replaces numbers with '#', and extracts the first number found in each word.
R Programming
text <- c("apple", "banana", "cherry123", "date")
pattern <- "[0-9]+"# Find which words have numbers
has_numbers <- grepl(pattern, text)
print(has_numbers)
# Replace numbers with '#'
replaced <- gsub(pattern, "#", text)
print(replaced)
# Extract numbers from words
numbers <- regmatches(text, regexpr(pattern, text))
print(numbers)
OutputSuccess
Important Notes
Regular expressions use special symbols like ., *, +, and [] to match patterns.
Use double backslashes \\ in R to escape special characters in patterns.
grepl() returns TRUE or FALSE, while regexpr() gives position info.
Summary
Regular expressions help find and change text patterns easily.
Use grepl() to check if a pattern exists.
Use sub() and gsub() to replace text based on patterns.
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.