Removing multiple spaces and trailing spaces using gsub

前端 未结 1 1646
粉色の甜心
粉色の甜心 2021-01-01 01:27

How can I remove multiple spaces and trailing spaces using only 1 gsub? I already made this function trim <- function(x) gsub(\' {2,}\',\' \',gsub(\'^ *| *$\',\'\',

相关标签:
1条回答
  • 2021-01-01 01:49

    Use a positive lookbehind to see if the current space is preceded by a space:

    ^ *|(?<= ) | *$
    

    See it here in action: http://regex101.com/r/bJ1mU0

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