Hello again, I was trying to extract the date element from a character vector using Regular expression. Below is a sample what I was trying to do:> Text <- c("asdf May 09 2009", "dsaf_01-01-2001") > gsub("[a-zA-Z_]", "", Text)[1] " 09 2009" "01-01-2001" I basically have a long text and the task is to extract the date element. My above strategy failed because I can not extract the month for the first element. Can somebody help me out? Thanks and regards,
Hello, This one seems to work. gsub("[[:alpha:]_]*(.*)", "\\1", Text) Hope this helps, Rui Barradas Em 18-01-2013 20:11, Christofer Bogaso escreveu:> Hello again, > > I was trying to extract the date element from a character vector using > Regular expression. Below is a sample what I was trying to do: > >> Text <- c("asdf May 09 2009", "dsaf_01-01-2001") >> gsub("[a-zA-Z_]", "", Text) > [1] " 09 2009" "01-01-2001" > > > I basically have a long text and the task is to extract the date > element. My above strategy failed because I can not extract the month > for the first element. > > Can somebody help me out? > > Thanks and regards, > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thanks Rui for your help. Could you also explain the underlying logic please? Thanks and regards, On Sat, Jan 19, 2013 at 2:43 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:> gsub("[[:alpha:]_]*(.*)", "\\1", Text)
Hi, Not sure what format you wanted the dates: gsub("^\\w+ ","",gsub("[_]"," ",Text)) #[1] "May 09 2009" "01-01-2001" #Another way is: gsub("^\\w+ |\\w+_","",Text) #[1] "May 09 2009" "01-01-2001" res<- gsub("^\\w+ |\\w+_","",Text) res1<-c(as.Date(res[grep(" ",res)],format="%b %d %Y"), as.Date(res[-grep(" ",res)],format="%m-%d-%Y")) res1 #[1] "2009-05-09" "2001-01-01" A.K. ----- Original Message ----- From: Christofer Bogaso <bogaso.christofer at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Friday, January 18, 2013 3:11 PM Subject: [R] Working with regular expression Hello again, I was trying to extract the date element from a character vector using Regular expression. Below is a sample what I was trying to do:> Text <- c("asdf May 09 2009", "dsaf_01-01-2001") > gsub("[a-zA-Z_]", "", Text)[1] "? 09 2009"? "01-01-2001" I basically have a long text and the task is to extract the date element. My above strategy failed because I can not extract the month for the first element. Can somebody help me out? Thanks and regards, ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.