Re: [R] Better use of regex

2016-09-15 Thread Bert Gunter
Thanks for the reproducible example. Using regular expressions: sub(".*HS_(.).*", "\\1", dimInfo[grep("HS_",dimInfo)]) The grep() gets just the indices that contain "HS_" and the sub() picks up the character you want from the subvector indexed by them and replaces everything with it. Cheers, Be

Re: [R] Better use of regex

2016-09-15 Thread ruipbarradas
Hello, What about the following? ff <- dimInfo[grep('HS', dimInfo)] sub("^.*HS_([[:alnum:]]).*$", "\\1", ff) Hope this helps, Rui Barradas Citando Doran, Harold : I have produced a terribly inefficient piece of codes. In the end, it gives exactly what I need, but clumsily steps through m

Re: [R] Better use of regex

2016-09-15 Thread Bob Rudis
Base: Filter(Negate(is.na), sapply(regmatches(dimInfo, regexec("HS_(.{1})", dimInfo)), "[", 2)) Modernverse: library(stringi) library(purrr) stri_match_first_regex(dimInfo, "HS_(.{1})")[,2] %>% discard(is.na) They both use capture groups to find the matches and return ju