prog
prog

Reputation: 1073

regex to remove alphanumeric from text

I am trying to remove the alphanumeric attached in front the text of words variable. I tried regex but not works.Please help.

words = c("A120 condition of returned veh","B520 vehivle of returned ve","petrol reserve switch")
Expected result = 
"condition of returned veh"
"vehivle of returned ve"
"petrol reserve switch"

Upvotes: 1

Views: 54

Answers (1)

akrun
akrun

Reputation: 887028

We can use gsub to match the pattern or zero or more digits (\\d*) followed by one or more alphabets and one or more digits as pattern and replace with blank ("")

gsub("\\b\\d*[A-Za-z]+\\d+\\s*", '', words)
#[1] "condition of returned veh" "vehivle of returned ve"    "petrol reserve switch" 

If we also need to remove numbers as well

gsub("\\b\\S*\\d+\\S*\\s", '', c(words, "120 condition of returned 35 veh"))
#[1] "condition of returned veh" "vehivle of returned ve"    "petrol reserve switch"     "condition of returned veh"

Upvotes: 1

Related Questions