[R] regex pattern assistance

Rui Barradas ruipbarradas at sapo.pt
Fri Aug 15 18:47:57 CEST 2014


Hello,

I don't believe you need an extra package for that. Try


sub("\\/mnt\\/AO\\/AO Data\\/([-[:alnum:]]*)\\/.+", "\\1", x)

or, with package stringr,

str_match(x,"\\/mnt\\/AO\\/AO Data\\/(.+)\\/.+")


Hope this helps,

Rui Barradas

Em 15-08-2014 17:18, Tom Wright escreveu:
> Hi,
> Can anyone please assist.
>
> given the string
>
>> x<-"/mnt/AO/AO Data/S01-012/120824/"
>
> I would like to extract "S01-012"
>
> require(stringr)
>> str_match(x,"\\/mnt\\/AO\\/AO Data\\/(.+)\\/+")
>> str_match(x,"\\/mnt\\/AO\\/AO Data\\/(\\w+)\\/+")
>
> both nearly work. I expected I would use something like:
>> str_match(x,"\\/mnt\\/AO\\/AO Data\\/([\\w -]+)\\/+")
>
> but I don't seem able to get the square bracket grouping to work
> correctly. Can someone please show me where I am going wrong?
>
> Thanks,
> Tom
>
> ______________________________________________
> 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