How to use regular expressions in R? Explain with 2 examples

This recipe helps you use regular expressions in R This recipe explains what with 2 examples

Recipe Objective

How to use regular expressions in R?

A regular expression is used to find certain patterns present in a string. A regular expression uses a few functions which return the matching pattern value as defined. Some of them are as follows: grep () — it returns the index value and the actual value of the matching string grepl () — returns Boolean values -TRUE/FALSE regexpr () — returns the index position of the very first matching string, etc. This recipe demonstrates an example of how to use regular expressions in R.

Step 1 - Define a vector

x <- c("street","sweet","tweet","breeze","freeze","sweet")

Step 2 - Use the regular expressions

grep("sweet|freeze", x) # grep - returns the index position of the given text values

 "Output of code is:"
2  5

x[grep("sweet|freeze", x)] # grep - returns the value at the position

 "Output of code is:"
'sweet' 'freeze'

grepl("sweet|freeze", x) # grepl - returns boolean values

 "Output of code is:"
FALSE  TRUE  FALSE  FALSE  TRUE  FALSE

regexpr("[S|s]weet", x) # regexpr - returns an integer vector with the same length as the input vector, and it returns -1 for values that dont match

 "Output of code is:"
-1  1-  1-  1-  1  1

{"mode":"full","isActive":false}

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.