R is a popular programming language used for data analysis and statistical computing. One of the most powerful features of R is its ability to work with regular expressions, or regex for short. Regular expressions are a sequence of characters that define a search pattern, allowing you to find and manipulate text in a flexible and efficient way.
If you’re new to R or regex, it can be overwhelming to remember all the different syntax and functions. That’s where a regex cheat sheet comes in handy. A regex cheat sheet is a quick reference guide that lists the most commonly used regex syntax and functions, along with examples and explanations.
The R regex cheat sheet covers everything from basic syntax like character classes and quantifiers, to more advanced concepts like lookarounds and backreferences. It also includes functions like grep(), gsub(), and sub(), which are used to search and replace text based on regex patterns.
Using a regex cheat sheet can save you time and frustration when working with text data in R. Instead of having to look up syntax and functions every time you need to perform a regex operation, you can simply refer to the cheat sheet for a quick reminder.
Overall, the R regex cheat sheet is a valuable resource for anyone working with text data in R. Whether you’re a beginner or an experienced user, it can help you become more efficient and effective in your data analysis and manipulation tasks.
This cheat sheet provides an extensive list of regular expressions (regex) in R. Regular expressions are used to match patterns in strings.
Basic Regular Expressions
Expression | Description |
---|---|
. | Matches any single character except newline |
^ | Matches the start of a string |
$ | Matches the end of a string |
[] | Matches any character inside the brackets |
[^] | Matches any character not inside the brackets |
| | Matches either the expression before or after the pipe |
() | Groups expressions together |
Quantifiers
Expression | Description |
---|---|
* | Matches zero or more occurrences of the preceding expression |
+ | Matches one or more occurrences of the preceding expression |
? | Matches zero or one occurrence of the preceding expression |
{n} | Matches exactly n occurrences of the preceding expression |
{n,} | Matches n or more occurrences of the preceding expression |
{n,m} | Matches between n and m occurrences of the preceding expression |
Character Classes
Expression | Description |
---|---|
\d | Matches any digit |
\D | Matches any non-digit |
\s | Matches any whitespace character |
\S | Matches any non-whitespace character |
\w | Matches any word character (letter, digit, or underscore) |
\W | Matches any non-word character |
Anchors
Expression | Description |
---|---|
\b | Matches a word boundary |
\B | Matches a non-word boundary |
\< | Matches the start of a word |
\> | Matches the end of a word |
Lookarounds
Expression | Description |
---|---|
(?=...) | Positive lookahead |
(?!...) | Negative lookahead |
(?<=...) | Positive lookbehind |
(?<!...) | Negative lookbehind |
Examples
Expression | Description |
---|---|
^[A-Z][a-z]+$ | Matches a string that starts with an uppercase letter followed by one or more lowercase letters |
^\d{3}-\d{2}-\d{4}$ | Matches a string that represents a social security number in the format XXX-XX-XXXX |
^([01]\d|2[0-3]):([0-5]\d)$ | Matches a string that represents a time in the format HH:MM in 24-hour clock notation |
^https?://[^\s/$.?#].[^\s]*$ | Matches a string that represents a URL starting with http or https |
Conclusion
This cheat sheet provides an extensive list of regular expressions in R. Regular expressions are a powerful tool for matching patterns in strings. By using regular expressions, you can quickly and easily extract information from text data.