Skip to content

Regexp: extract first occurrence of date in string

5 messages · johannes rara, Gabor Grothendieck

#
I would like to extract first date from a string:
[1] "first date is 05.12.2009. Second date is 06.12.2009."

I tried:
[1] "05.12.2009. Second date is 06.12.2009."
How to modify this?

-J
#
Try this which uses a slightly simpler regexp:
[1] "05.12.2009"

or we could convert it to Date class at the same time where we have
assumed month.day.year:
[1] "2009-05-12"

or this even simpler regexp extracting all the dates and then picking
off the first:
[1] "05.12.2009"
On Sat, Jan 2, 2010 at 10:08 AM, johannes rara <johannesraja at gmail.com> wrote:
#
Thanks, is the same possible using basic gsub/sub/grep etc. functions?

-J

2010/1/2 Gabor Grothendieck <ggrothendieck at gmail.com>:
#
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 <johannesraja at gmail.com> wrote:
#
Thanks for the hint, ie. something like this works in this case:
[1] "first date is 05.12.2009. Second date is 06.12.2009."
[1] "05.12.2009"
But your examples are more generic. I'll have to look gsubfn more closely.

-J

2010/1/2 Gabor Grothendieck <ggrothendieck at gmail.com>: