[R] Regex for Special Characters under Grep

Marc Schwartz marc_schwartz at comcast.net
Fri Jun 13 04:06:42 CEST 2008


on 06/12/2008 08:42 PM Gundala Viswanath wrote:
> Hi all,
> 
> I am trying to capture lines of a file that DO NOT
> start with the following header: !, #, ^
> 
> But somehow my regex used under grep doesn't
> work.
> 
> Please advice what's wrong with my code below.
> 
> __BEGIN__
> in_fname <- paste("mydata.txt,".soft",sep="")
> data_for_R <- paste("data_for_R/", args[3], ".softR", sep="")
> 
> # my regex construction
> cat(temp[-grep("^[\^\!\#]",temp,perl=TRUE)], file=data_for_R, sep="\n")
> 
> 
> dat <- read.table(data_for_R)
> ___END__
> 

You need to double the escape character when being used to differentiate 
meta-characters in a regex. Note also that the only meta-character in 
your sequence is the carat ('^').

Lines <- c("! Not This Line", "# Not This Line", "^ Not This Line",
            "This Line")

 > Lines
[1] "! Not This Line" "# Not This Line" "^ Not This Line"
[4] "This Line"

 > grep("^[!#\\^]", Lines)
[1] 1 2 3

 > Lines[-grep("^[!#\\^]", Lines)]
[1] "This Line"


HTH,

Marc Schwartz



More information about the R-help mailing list