0
0
R Programmingprogramming~5 mins

Regular expressions in R in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aregmatches()
Bgrepl()
Cgsub()
Dgregexpr()
What does the regex symbol \d represent in R?
AAny letter
BAny whitespace
CAny digit (0-9)
DStart of string
Which function would you use to replace text matching a regex pattern in R?
Agsub()
Bgregexpr()
Cgrepl()
Dregexpr()
What does the regex pattern abc$ mean?
AString ends with 'abc'
BString starts with 'abc'
CString contains 'abc' anywhere
DString has 'abc' repeated
Which function returns TRUE or FALSE if a pattern is found in a string?
Agsub()
Bgrepl()
Cregmatches()
Dgregexpr()
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.