[R] Regular expression \ String Extraction help

Tony Breyal tony.breyal at googlemail.com
Wed Jun 3 13:34:16 CEST 2009


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




More information about the R-help mailing list