user3310782
user3310782

Reputation: 831

grep exact match excluding certain terms

Help apprecited. I've got a long list of column names in a vector, where the pattern is like this:

cols <-c('a35-male-x1','a35-female-x2','a16-male-t0','a65-female-t7',...)

I've tried using grep to find the columns with 'male' matches, but exclude the female ones.

grep(pattern='male',cols)

will get all 4 entries. Is there a simple way to get this done in grep? Or a better approach using some other function? Thanks in advance, p.

Upvotes: 0

Views: 369

Answers (4)

Mike V
Mike V

Reputation: 1364

Here is another approach

grep("[^female][male]", cols, value = TRUE)
#[1] "a35-male-x1" "a16-male-t0"

Upvotes: 0

Colonel Beauvel
Colonel Beauvel

Reputation: 31171

Another alternative (considering you only have male or female):

Filter(function(u) !grepl('female',u), cols)
#[1] "a35-male-x1" "a16-male-t0"

Or simply:

cols[!grepl('female',cols)]

Upvotes: 2

vks
vks

Reputation: 67968

(?<!fe)male

You can use the with perl=True option to get exactly what you want

Upvotes: 2

akrun
akrun

Reputation: 887108

You can use word boundary i.e \\b to match only 'male'

grep('\\bmale\\b', cols, value=TRUE)
#[1] "a35-male-x1" "a16-male-t0"

For this example,

grep('-male-', cols)
#[1] 1 3

would also work

data

 cols <-c('a35-male-x1','a35-female-x2','a16-male-t0','a65-female-t7')

Upvotes: 3

Related Questions