0

This what I have:

f=5.20
y=168.9850
dat=c("dat.txt","dat_5.20.txt","data_5.20_168.9850.txt")
Filter(function(x) grepl(f, x), dat)
# [1] "dat_5.20.txt"           "data_5.20_168.9850.txt"

I need to grep only the one obtained f and y

How to use both f and y in grepl?

The desired result would be:

"data_5.20_168.9850.txt"
Jaap
  • 81,064
  • 34
  • 182
  • 193
temor
  • 935
  • 2
  • 10
  • 26

3 Answers3

2

One pure regex way of doing this would be to just use two lookahead assertions which independently check for the presence of each of the number strings:

f <- "5\\.20"
y <- "168\\.9850"
dat <- c("dat.txt","dat_5.20.txt","data_5.20_168.9850.txt")

grepl(paste0("(?=.*", f, ")(?=.*", y, ")"), dat, perl=TRUE)

[1] FALSE FALSE  TRUE

The pattern used here is (?=.*5\.20)(?=.*168\.9850).

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • @MikeH. No, we don't. Every regex API is different, and some match the entire input by default while others don't. Hard to keep track of this. – Tim Biegeleisen Feb 26 '18 at 16:00
2

I suppose if you had a long set of search strings and you didn't want to have to type out everything you could do:

dat[Reduce("&", lapply(c(f,y), function(x, dat) grepl(x, dat), dat = dat))]

However, you could probably also get around typing everything out using @TimBiegeleisen's method by doing something like: paste0("(?=.*", c(f,y), ")", collapse = "") and using the result as your search string.

Mike H.
  • 13,960
  • 2
  • 29
  • 39
  • Both of these could be a little less wordy. `dat[Reduce("&", lapply(c(f,y), grepl, x = dat))]` and `paste0("(?=.*", c(f,y), ")", collapse = '')` – IceCreamToucan Feb 26 '18 at 16:02
  • @Renu, good point about the second. For the first I did it that way so that `dat` was not hardcoded into the function. It is passed as a parameter instead – Mike H. Feb 26 '18 at 16:18
1

We can do two grep's using any of these alternatives:

grep(y, grep(f, dat, value = TRUE), value = TRUE)
## [1] "data_5.20_168.9850.txt"

dat[grepl(f, dat) & grepl(y, dat)]
## [1] "data_5.20_168.9850.txt"

dat[ intersect(grep(f, dat), grep(y, dat)) ]
## [1] "data_5.20_168.9850.txt"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341