Re: [R] Regexp: extract first occurrence of date in string

2010-01-02 Thread johannes rara
Thanks for the hint, ie. something like this works in this case: > txt <- "first date is 05.12.2009. Second date is 06.12.2009." > txt [1] "first date is 05.12.2009. Second date is 06.12.2009." > l <- regexpr("\\d{1,2}\\.\\d{1,2}\\.\\d{4}", txt, perl=T) > substr(txt, l, l+9) [1] "05.12.2009" > Bu

Re: [R] Regexp: extract first occurrence of date in string

2010-01-02 Thread Gabor Grothendieck
Use regexpr to get the offset into the string and its length and then use substr to pick extract it. On Sat, Jan 2, 2010 at 10:43 AM, johannes rara wrote: > Thanks, is the same possible using basic gsub/sub/grep etc. functions? > > -J > > 2010/1/2 Gabor Grothendieck : >> Try this which uses a sli

Re: [R] Regexp: extract first occurrence of date in string

2010-01-02 Thread johannes rara
Thanks, is the same possible using basic gsub/sub/grep etc. functions? -J 2010/1/2 Gabor Grothendieck : > Try this which uses a slightly simpler regexp: > >> library(gsubfn) >> strapply(txt, "(\\d{1,2}\\.\\d{1,2}\\.\\d{4}).*")[[1]] > [1] "05.12.2009" > > or we could convert it to Date class at th

Re: [R] Regexp: extract first occurrence of date in string

2010-01-02 Thread Gabor Grothendieck
Try this which uses a slightly simpler regexp: > library(gsubfn) > strapply(txt, "(\\d{1,2}\\.\\d{1,2}\\.\\d{4}).*")[[1]] [1] "05.12.2009" or we could convert it to Date class at the same time where we have assumed month.day.year: > strapply(txt, "(\\d{1,2}\\.\\d{1,2}\\.\\d{4}).*", ~ as.Date(x,

[R] Regexp: extract first occurrence of date in string

2010-01-02 Thread johannes rara
I would like to extract first date from a string: > txt <- "first date is 05.12.2009. Second date is 06.12.2009." > txt [1] "first date is 05.12.2009. Second date is 06.12.2009." I tried: > sub("^.*?\\s(\\d{1,2}\\.\\d{1,2}\\.\\d{4})", "\\1", txt, extended=T, perl=T) [1] "05.12.2009. Second date