Here’s a R regex cheat sheet for using regular expressions in R:
Basics
Matching a Pattern:
grepl("pattern", text)
Extracting Matches:
regmatches(text, gregexpr("pattern", text))
Anchors and Quantifiers
Start of Line:
"^pattern"
End of Line:
"pattern$"
Zero or More Occurrences:
"a*"
One or More Occurrences:
"a+"
Optional (Zero or One):
"a?"
Character Classes
Any Digit:
"\\d"
Any Word Character:
"\\w"
Any Whitespace Character:
"\\s"
Negation:
"[^0-9]"
Quantifiers
Exact Number of Occurrences:
"a{2}"
Range of Occurrences:
"a{2,4}"
Groups and Alternation
Grouping:
"(abc)+"
Alternation:
"cat|dog"
Escape Characters
Escape Special Characters:
"\\."
Escape Metacharacters:
"\\(\\[\\{"
Modifiers
Case Insensitive:
"(?i)pattern"
Multiline Mode:
"(?m)pattern"
Anchors
Word Boundary:
"\\bword\\b"
Negative Word Boundary:
"\\Bword\\B"
This cheat sheet covers some commonly used regex patterns and constructs in R. Regular expressions in R follow the POSIX syntax, and the grep
, grepl
, regexpr
, and regmatches
functions are commonly used for regex operations. Feel free to refer to the R documentation on regular expressions for more details and advanced usage.