Skip to content
Back to formatted view

Raw Message

Message-ID: <971536df1001020735u33a9fbb3w5c34dfb73ccf079@mail.gmail.com>
Date: 2010-01-02T15:35:10Z
From: Gabor Grothendieck
Subject: Regexp: extract first occurrence of date in string
In-Reply-To: <f49fcd831001020708j2040f5cq3da421ce6698026a@mail.gmail.com>

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, "%m.%d.%Y"))[[1]]
[1] "2009-05-12"

or this even simpler regexp extracting all the dates and then picking
off the first:

> strapply(txt, "\\d{1,2}\\.\\d{1,2}\\.\\d{4}")[[1]][1]
[1] "05.12.2009"

On Sat, Jan 2, 2010 at 10:08 AM, johannes rara <johannesraja at gmail.com> wrote:
> 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 is 06.12.2009."
>>
>
> How to modify this?
>
> -J
>
> ______________________________________________
> 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.
>