简体   繁体   中英

What is the way to match ALL patterns in strings using grep() in R?

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 (eg 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.

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"

An easy solution would be to combine two grep with 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM