0

My question is how to do what the answer of [this question][1] does, but only matching when the pattern ends with the end of the string.

my idea was adding $ after the pattern, to indicate the end of the line, exemple:

removePunctuation <- function(punctuationObject){
  itemsToBeRemoved <- c(".", ",", ";", ":", "'", "!", "#", "-", "--")
  resultObject <- punctuationObject
  for (itemToReplace in itemsToBeRemoved){
    resultObject <- gsub(itemToReplace$, "", resultObject, fixed = TRUE)
  }
  resultObject
}

but it doesnt work. i tried many variations of the same thing, but i cant get the $ to be read as regex after stating a variable pattern such as the 'itemToReplace'. [1]: For loop over list of items to use in R's gsub

ubuntunoob
  • 29
  • 7
  • 1
    You have to `paste0` the dollar symbol onto the `itemToReplace` object. But you will have difficulty because you have `fixed=TRUE`. ;| – Edward Feb 21 '23 at 08:23

2 Answers2

1

First, you can use sub instead of gsub. Second, you need to paste the $ onto the end of the itemsToBeRemoved. But since $ is a metacharacter, you cannot use fixed=TRUE in your gsub call. One solution to that problem is to enclose the punctuation items within []:

removePunctuation <- function(punctuationObject){
  itemsToBeRemoved <- c(".", ",", ";", ":", "'", "!", "#", "-", "--")
  resultObject <- punctuationObject
  for (itemToReplace in itemsToBeRemoved){
    resultObject <- sub(paste0("[", itemToReplace, ']$'), "", resultObject)
  }
  resultObject
}

Test:

removePunctuation("Hello, I use R.")
# [1] "Hello, I use R"
Edward
  • 10,360
  • 2
  • 11
  • 26
  • i have an object with multiple strings, so i do need to use gsub, should i update the question? – ubuntunoob Feb 21 '23 at 09:00
  • I don't think so because, in your loop, you are selecting only one item from the multi-item string object. The other poster agrees. – Edward Feb 21 '23 at 09:06
1

I would use sub for that task to remove variables at the end of the line.

s <- c("x", ".x", "x.", "x--x", "x--", "x---")

sub("(--|[.,;:'!#-])$", "", s)
#[1] "x"    ".x"   "x"    "x--x" "x"    "x-"
GKi
  • 37,245
  • 2
  • 26
  • 48