Michael
Michael

Reputation: 19

Function for regular expression in R

I need to extract certain sequences from a string of text. Something like 93085k82 will be embedded in text. Is there a script that identify when 5 numbers, a letter, and then 2 numbers occur?

Upvotes: 0

Views: 39

Answers (1)

akrun
akrun

Reputation: 887691

We can use pattern starting with word boundary (\\b) followed by five digits (\\d{5}), a lower case letter ([a-z]{1}) and two digits (\\d{2}) followed by the word boundary (\\b)

grep("\\b\\d{5}[a-z]{1}\\d{2}\\b", v1)

If we need to extract

library(stringr)
str_extract_all(v1, "\\b\\d{5}[a-z]{1}\\d{2}\\b")

Upvotes: 1

Related Questions