joaoal
joaoal

Reputation: 1992

Return only matched pattern from grep

given the follwing example in R:

my.list<-list(a='ivw_2014_abc.pdf',b='ivw_2014_def.pdf',c='ivw_2014_ghi.pdf')

grep('(?<=ivw_2014_)[a-z]*',my.list,perl=T,value=T)

returns

                 a                  b                  c
"ivw_2014_abc.pdf" "ivw_2014_def.pdf" "ivw_2014_ghi.pdf"

I would like to make it return only

[1] 'abc' 'def' 'ghi'

in bash I would use the -o option. How do I achieve this in R?

Upvotes: 4

Views: 1122

Answers (5)

Greg Snow
Greg Snow

Reputation: 49640

Look at the regmatches function. It works with regexpr rather than grep, but returns just the matched part of the string.

Upvotes: 0

akrun
akrun

Reputation: 887108

Another option would be:

library(stringi)
stri_extract_first_regex(unlist(my.list), "[A-Za-z]+(?=\\.)")
#[1] "abc" "def" "ghi"

Upvotes: 0

rnso
rnso

Reputation: 24535

Following may be of interest:

as.character(unlist(data.frame(strsplit(as.character(unlist(data.frame(strsplit(as.character(my.list),'\\.'))[1,])), '_'))[3,]))
[1] "abc" "def" "ghi"

Following is easier to read:

as.character(
    unlist(data.frame(strsplit(as.character(
        unlist(data.frame(strsplit(as.character(
            my.list),'\\.'))[1,])), '_'))[3,]))
[1] "abc" "def" "ghi"

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

Without using any capturing groups,

> my.list<-list(a='ivw_2014_abc.pdf',b='ivw_2014_def.pdf',c='ivw_2014_ghi.pdf')
> gsub("^.*_|\\..*$", "", my.list, perl=T)
[1] "abc" "def" "ghi"

Upvotes: 3

agstudy
agstudy

Reputation: 121568

For example :

sub('.*_(.*)[.].*','\\1',my.list)
[1] "abc" "def" "ghi"

Upvotes: 2

Related Questions