[R] Regular expression \ String Extraction help

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 3 16:11:58 CEST 2009


With

txt <- c("2009-04-10 1400 Fri Foo1.txt", "2009-04-10 1400 Fri
Universities2.txt",
"2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt")

# Try this line:

> sub("(.*) ([^0-9]+)([0-9]+).txt", "C:\\\\test\\\\\\2\\\\\\1 \\2\\3.txt", txt)
[1] "C:\\test\\Foo\\2009-04-10 1400 Fri Foo1.txt"
[2] "C:\\test\\Universities\\2009-04-10 1400 Fri Universities2.txt"
[3] "C:\\test\\Galaxy\\2009-04-10 1400 Fri Hitchhikers Guide To The
Galaxy42.txt"


On Wed, Jun 3, 2009 at 7:34 AM, Tony Breyal <tony.breyal at googlemail.com> wrote:
> Dear all,
>
> Is there a good way of doing the following conversion:
>
> [YYYY]-[MM]-[DD] [Time] [Day] [Name][Integer].[Extention]
>
> to become
>
> C:\test\[Name]\[YYYY]-[MM]-[DD] [Time] [Day]\[YYYY]-[MM]-[DD] [Time]
> [Day] [Name][Integer].[Extention]
>
> i.e. these
>
> 2009-04-10 1400 Fri Foo1.txt
> 2009-04-10 1400 Fri Universities2.txt
> 2009-04-10 1400 Fri Hitchhikers Guide To The Galaxy42.txt
>
> will become
>
> C:\test\Foo\2009-04-10 1400 Fri Foo1.txt
> C:\test\Universities\2009-04-10 1400 Fri Universities2.txt
> C:\test\Hitchhikers Guide To The Galaxy\2009-04-10 1400 Fri
> Hitchhikers Guide To The Galaxy42.txt
>
> My main issue is the conversion for 'Hitchkikers Guide To The
> Galaxy54' because of the spaces in the Name. So far this is what i
> have:
>
>> txt <- '2009-04-10 1400 Fri Universities1.txt'
>> step1 <- unlist(strsplit(txt, '\\.'))
>> step2 <- unlist(strsplit(step1[1], ' '))
>> Name <- gsub('[0-9]',replacement='', step2[4])
>> step3 <- paste(step2[1], step2[2], step2[3], sep=' ')
>> paste('C:\\test\\', Name, '\\', step3, '\\', txt, sep='' )
> [1] "C:\\test\\Universities\\2009-04-10 1400 Fri\\2009-04-10 1400 Fri
> Universities1.txt"
>
> Cheers,
> Tony Breyal
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list