Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove word from different positions

Tags:

string

regex

r

I am pretty regex-blind so I was looking for a way to uniformly remove the word remove from the following string

x <- c('something,remove', 'remove, something', 'something, remove, somethingElse,alsoThis')

and get the result, 'something', 'something', 'something, somethingElse, alsoThis'

I can do it with strsplit but I was wondering for the regex version too

sapply(strsplit(x, ', |,'), function(i)paste(i[i != 'remove'], collapse = ', '))
#[1] "something"    "something"   "something, somethingElse, alsoThis"
like image 925
Sotos Avatar asked Oct 25 '25 17:10

Sotos


1 Answers

Here is a base R solution using gsub():

x <- c('something,remove', 'remove, something', 'something, remove, somethingElse,alsoThis')
output <- gsub("^,|,$", "", gsub(",?\\s*\\bremove,?\\s*", ",", x))
output

[1] "something"                        "something"
[3] "something,somethingElse,alsoThis"

The inner call to gsub() removes remove along with leading/trailing whitespace and commas. The outer call to gsub() is a cleanup to remove any possible leading/trailing commas.

like image 107
Tim Biegeleisen Avatar answered Oct 27 '25 07:10

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!