-2

I have a vector

    c("SRSF1_IP_tmt_kit_2hours_04_4ul.raw", "SRSF1_IP_tmt_kit_2hours_01_4ul.raw", 
  "SRSF1_IP_tmt_kit_2hours_01_1ul.raw", "SRSF1_IP_tmt_kit_2hours_04_1ul.raw", 
  "SRSF1_IP_tmt_beads_2hours_01_4ul.raw", "SRSF1_IP_tmt_beads_2hours_01_1ul.raw", 
"SRSF1_IP_tmt_beads_2hours_02_1ul.raw", "SRSF1_IP_tmt_beads_2hours_02_4ul.raw", 
  "SRSF1_IP_tmt_beads_2hours_03_4ul.raw","SRSF1_IP_tmt_beads_2hours_03_1ul.raw", 
  "SRSF1_IP_tmt_beads_2hours_04_1ul.raw","SRSF1_IP_tmt_beads_2hours_04_4ul.raw", 
  "SRSF1_IP_tmt_kit_2hours_02_1ul.raw", "SRSF1_IP_tmt_kit_2hours_02_4ul.raw", 
  "SRSF1_IP_tmt_kit_2hours_03_4ul.raw", "SRSF1_IP_tmt_kit_2hours_03_1ul.raw"
)

I would like to get the indices of elements that have BOTH "4ul" AND "kit" word. I have looked at many tutorials on regular expressions (e.g. https://www.rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf) and they only show how to match either of the two (using "|") , but not both.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Pavel Shliaha
  • 773
  • 5
  • 16
  • 1
    Could you also add what you tried already? Like the actual code that you so such solutions can be ruled out and/or to show your efforts. – NelsonGon Jul 15 '19 at 16:23

2 Answers2

3

We can use .* to specify characters between '4ul' 'kit' or between 'kit' followed by '4ul'

i1 <- grep("4ul.*kit|kit.*4ul", v1)
v1[i1]
#[1] "SRSF1_IP_tmt_kit_2hours_04_4ul.raw" "SRSF1_IP_tmt_kit_2hours_01_4ul.raw" "SRSF1_IP_tmt_kit_2hours_02_4ul.raw"
#[4] "SRSF1_IP_tmt_kit_2hours_03_4ul.raw"
akrun
  • 874,273
  • 37
  • 540
  • 662
1

An easy solution would be to combine two grepwith an & like:

x[grepl("4ul", x) & grepl("kit", x)]
#[1] "SRSF1_IP_tmt_kit_2hours_04_4ul.raw" "SRSF1_IP_tmt_kit_2hours_01_4ul.raw"
#[3] "SRSF1_IP_tmt_kit_2hours_02_4ul.raw" "SRSF1_IP_tmt_kit_2hours_03_4ul.raw"

and the indices can come from which:

which(grepl("4ul", x) & grepl("kit", x))
#[1]  1  2 14 15

or you use a non-consuming regular expression.

grep("(?=.*4ul)(?=.*kit)", x, perl=TRUE)
#[1]  1  2 14 15

Have a look at: Regular Expressions: Is there an AND operator? or Regex AND operator.

GKi
  • 37,245
  • 2
  • 26
  • 48