0
0
R Programmingprogramming~10 mins

Regular expressions in R in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the string contains the word 'cat'.

R Programming
grepl([1], "The cat is sleeping")
Drag options to blanks, or click blank then click option'
A"cat"
B"dog"
C"bird"
D"fish"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the pattern.
Using a word not present in the string.
2fill in blank
medium

Complete the code to extract all words starting with 'b' from the string.

R Programming
regmatches(text, gregexpr([1], text))
Drag options to blanks, or click blank then click option'
A"b"
B"\w+b"
C"b+"
D"\bb\w+"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using word boundary \b.
Using incorrect quantifiers.
3fill in blank
hard

Fix the error in the pattern to match email addresses.

R Programming
grepl([1], email)
Drag options to blanks, or click blank then click option'
A"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"
B"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}"
C"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
D"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2}"
Attempts:
3 left
💡 Hint
Common Mistakes
Not escaping the dot correctly.
Using single backslash instead of double.
4fill in blank
hard

Fill both blanks to create a named vector with word lengths for words longer than 3 letters.

R Programming
word_lengths <- c([1] = nchar([2]))
Drag options to blanks, or click blank then click option'
Awords
Bword
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the names and values.
Using the wrong variable names.
5fill in blank
hard

Fill all three blanks to create a list of words longer than 4 letters with their uppercase versions.

R Programming
result <- list([1] = [2], [3] = toupper([2]))
Drag options to blanks, or click blank then click option'
Along_words
Bwords
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Mixing up the names of list elements.