0
0
R Programmingprogramming~7 mins

Regular expressions in R in R Programming

Choose your learning style9 modes available
Introduction

Regular expressions help you find patterns in text. They make searching and changing text easier and faster.

You want to check if a word or phrase appears in a sentence.
You need to extract phone numbers or emails from a list of text.
You want to replace all spaces with dashes in a string.
You want to split a sentence into words based on punctuation.
You want to validate if a text matches a specific format, like a date.
Syntax
R Programming
grepl(pattern, text)
sub(pattern, replacement, text)
gsub(pattern, replacement, text)
regexpr(pattern, text)
regmatches(text, regexpr(pattern, text))

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.