Making gsub only replace entire words?

后端 未结 2 831
我在风中等你
我在风中等你 2020-12-17 20:41

(I\'m using R.) For a list of words that\'s called \"goodwords.corpus\", I am looping through the documents in a corpus, and replacing each of the words on the list \"goodwo

相关标签:
2条回答
  • 2020-12-17 20:56

    Use \b to indicate a word boundary:

    > text <- "good night goodnight"
    > gsub("\\bgood\\b", paste("good", 1234), text)
    [1] "good 1234 night goodnight"
    

    In your loop, something like this:

    for (word in goodwords.corpus){
      patt <- paste0('\\b', word, '\\b')
      repl <- paste(word, "1234")
    
      test <-gsub(patt, repl, test)
    }
    
    0 讨论(0)
  • 2020-12-17 20:58

    You are so close to getting this. You're already using paste to form the replacement string, why not use it to form the pattern string?

    goodwords.corpus <- c("good")
    test <- "I am having a good time goodnight"
    for (i in 1:length(goodwords.corpus)){
        test <-gsub(paste0('\\<', goodwords.corpus[[i]], '\\>'), paste(goodwords.corpus[[i]], "1234"), test)
    }
    test
    # [1] "I am having a good 1234 time goodnight"
    

    (paste0 is merely paste(..., sep='').)

    (I posted this the same time as @MatthewLundberg, and his is also correct. I'm actually more familiar with using \b vice \<, but I thought I'd continue with using your code.)

    0 讨论(0)
提交回复
热议问题