Home > OS >  Regex to remove word from different positions
Regex to remove word from different positions

Time:06-24

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"

CodePudding user response:

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.

CodePudding user response:

A possible solution, based on stringr::str_remove:

library(stringr)

str_remove(x, "\\,*remove\\,*\\s*")

#> [1] "something"                         "something"                        
#> [3] "something, somethingElse,alsoThis"
  • Related