[R] regex pattern assistance

S Ellison S.Ellison at LGCGroup.com
Fri Aug 15 18:36:57 CEST 2014



> -----Original Message-----
> > x<-"/mnt/AO/AO Data/S01-012/120824/"
> 
> I would like to extract "S01-012"


> gsub("/mnt/AO/AO Data/(.+)/.+", "\\1", x)

#does it, as does
> gsub("/mnt/AO/AO Data/([\\w-]+)/.+", "\\1", x, perl=TRUE)    # \w is perl RE; the default is POSIX, which would be. 
> gsub("/mnt/AO/AO Data/([[:alnum:]-]+)/.+", "\\1", x)  

#and
> str_match(x,"/mnt/AO/AO Data/(.+)/.+")
> str_match(x,"/mnt/AO/AO Data/([[:alnum:]-]+)/.+")   #again, needs POSIX, not perl RE

You had also, btw, missed the '.' in the closing '.+', meaning that your regex was looking for '/+', that is, multiple instances of '/'

Steve E



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list