How can I remove multiple spaces and trailing spaces using only 1 gsub? I already made this function trim <- function(x) gsub(' {2,}',' ',gsub('^ *| *$','',x))
, but i'm trying to rewrite it with only 1 gsub.
Actually, I want lean how to match something based in what is after/before it with gsub. In this example I need to match all spaces that are preceeded by a single space, and replace them by ''
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With