Skip to content
Back to formatted view

Raw Message

Message-ID: <49835340.3050106@idi.ntnu.no>
Date: 2009-01-30T19:21:36Z
From: Wacek Kusnierczyk
Subject: sub question
In-Reply-To: <979ffa270901300924l6ce35826i367db0907c49b208@mail.gmail.com>

David Hajage wrote:
> Hello R users,
>
> I have a string, for example:
> x <- "\t\tabc\t def"
>
> This string can contain any number of tabulations. I want to replace each
> tabulation of the begining of the string by the same number of space:
> "  abc\t def"
>
> I'm trying to do this with gsub :
>   
>> gsub("\t", " ", x) # replace every \t
>>     
> [1] "  abc  def"
>   
>> sub("^\t", " ", x) # replace only the first \t
>>     
> [1] " \tabc\t def"
>   
>> sub("^\t*", " ", x) # replace all beginning \t, but only one space
>>     
> [1] " abc\t def"
>
> How can I do this ?
>
>   

there may be better solutions, but this should do:

strings = c('pure', '\t<- 1 removed', '\t\t<- 2 removed, 1 left ->\t')
gsub('\t|(*COMMIT)(*FAIL)', ' ', strings, perl=TRUE)
# "pure" " <- 1 removed" "  <- 2 removed, 1 left ->\t"

vQ