Skip to content
Prev 173723 / 398503 Next

gsub and regex to tidy comma-limited values

Tom
sub(",$","",gsub("^,*|,*$|(,)*", "\\1", 
",,,apple,,orange,,,,,lemon,strawberry,,,,"))

It may not be the best solution, but it was my first thought

Tom

----- Original Message ----- 
From: "Daren Tan" <darentan76 at gmail.com>
To: <r-help at r-project.org>
Sent: Saturday, March 14, 2009 6:42 PM
Subject: [R] gsub and regex to tidy comma-limited values