Home > Back-end >  How to use grepl on only part of a string
How to use grepl on only part of a string

Time:02-11

Vector<-c("Consider criterion1, criterion2, criterion3, stop considering criterion1,criterion2, criterion3")
Vector2<-c("Consider criterion2, criterion3, stop considering criterion1,criterion2, criterion3")

grepl("criterion1",Vector)
[1] TRUE

For this second condition I want to have FALSE as I would like to ignore all characters after the the word stop

grepl("criterion1",Vector2)
[1] FALSE

CodePudding user response:

Few ways to tackle this:

You could remove everything after stop by using sub, to ensure that you only check before stop

grepl('criterion1', sub('stop.*', '', Vector))
[1]  TRUE 

grepl('criterion1', sub('stop.*', '', Vector2))
[1] FALSE

Or you could change the pattern altogether to ensure there is no stop before the value being checked.

grepl('^((?!stop).)*criterion1', Vector, perl = TRUE)
[1] TRUE

grepl('^((?!stop).)*criterion1', Vector2, perl = TRUE)
[1] FALSE

Note that grepl is vectorized on x hence we could simply do:

grepl('^((?!stop).)*criterion1', c(Vector, Vector2), perl = TRUE)
[1]  TRUE FALSE
  • Related