0

Actually i was looking for if there is multiple pattern matching with grepl(), sub() and so on say something like grepl(...multiple=TRUE). I have a list of files and want to check if multiple extension are existing in the files. For a single extension i use grepl("*.cel$",lst) where lst is list of files. It returns TRUE or FALSE. I have a list of "1.txt" "2.txt" "3.txt" "4.txt" "final.csv""final1.csv" "final2.csv" "GSM248238.CEL" "GSM248650.CEL" "GSM248651.CEL" "GSM248652.CEL" "GSM248653.CEL" "GSM248655.CEL" "GSM248659.CEL" "GSM248660.CEL" "GSM248661.CEL"

Now if i am interested to check something like c(".cel",".txt",".csv"). I thought to try grepl(c("*.cel$",".txt$",".csv$"), ignore.case=TRUE), it gives the extension of first(.cel). Other extension are also present, how to get all the extensions.

Agaz Wani
  • 5,514
  • 8
  • 42
  • 62
  • 2
    You can use `paste`, i.e. `grepl(paste('.cel$', '.txt$', .csv$', collapse='|'), yourfiles, ignore.case=TRUE)` – akrun May 14 '15 at 18:05
  • @akrun Returns `FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE` – Agaz Wani May 14 '15 at 18:09
  • The bar, `|`, operator is equivalent to `or` in R. `'*.cel$' or '*.txt$'` in regex is `'*.cel$ | *.txt$'`. – Pierre L May 14 '15 at 18:10
  • @akrun just needs a single quote before `.csv$'` – Pierre L May 14 '15 at 18:10
  • I didn't test it, but I think some variant of that should work, @plafort thanks a typo there – akrun May 14 '15 at 18:11
  • 1
    `"\\.(cel|txt|csv)$"` because `.` is a wildcard character. – Frank May 14 '15 at 18:11
  • 1
    I should have used `c`, i.e. `paste(c('.cel$', '.txt$', '.csv$'), collapse='|')` – akrun May 14 '15 at 18:12
  • I swear this is a duplicate but I can't find the question. :/ Basically, the pattern argument to the regular expression functions in R isn't vectorized, so you need to collapse it into a single regex pattern as akrun and Frank have done. – Alex A. May 14 '15 at 18:12
  • @plafort '.csv$' is not the case . not working either – Agaz Wani May 14 '15 at 18:14
  • If you are constructing your list from the file structure: http://stackoverflow.com/questions/4876813/using-r-to-list-all-files-with-a-specified-extension – Frank May 14 '15 at 18:15
  • @akrun Looking for a dupe :) – Frank May 14 '15 at 18:15
  • 1
    @Frank: David already marked this as a dupe. – Alex A. May 14 '15 at 18:16
  • @AlexA. Ah thanks. The page didn't alert me or update itself. – Frank May 14 '15 at 18:17
  • @Frank, Will U not post your solution – Agaz Wani May 14 '15 at 18:26
  • @AaghazHussain Because this question (or something very close to it) has been asked before, it is closed to new answers. Refresh the page and you'll see the so-called "duplicate" question linked in a yellow header. My solution in the comment will still be here for anyone who comes across your question later. Here's the help page: http://stackoverflow.com/help/duplicates – Frank May 14 '15 at 18:29
  • @Frank I already saw the duplicate mark. Thanks – Agaz Wani May 14 '15 at 18:31
  • @Frank I have a similar problem dealing with multiple extensions. i tried `"\\.(cel|txt|csv)$"` as per your comment above and it return `TRUE` if files with any of the extension are present, however i am interested to check if all the three files or more are present.How to do that. – Hashim Jun 12 '15 at 16:23
  • I'm not sure I follow. You can post a new question. Answers may come pretty slowly on the weekend, though. – Frank Jun 12 '15 at 16:30

0 Answers0