0

Regex has a character for or but what is the character for and?

For example, say I have a folder with the following files:

list.files(path = 'example')
# [1] "abc_clean.csv"           "abc_other_clean.csv"     "abc_other_raw.csv"      
# [4] "abc_raw.csv"             "abc_something_clean.csv" "abc_something_raw.csv"  
# [7] "def_clean.csv"           "def_other_clean.csv"     "def_other_raw.csv"      
# [10] "def_raw.csv"             "def_something_clean.csv" "def_something_raw.csv" 

And I want to match the files that both have `abc' and 'clean'. None of these work:

> list.files(path = 'example', pattern = 'abc')
[1] "abc_clean.csv"           "abc_other_clean.csv"     "abc_other_raw.csv"      
[4] "abc_raw.csv"             "abc_something_clean.csv" "abc_something_raw.csv"  

> list.files(path = 'example', pattern = 'clean')
[1] "abc_clean.csv"           "abc_other_clean.csv"     "abc_something_clean.csv"
[4] "def_clean.csv"           "def_other_clean.csv"     "def_something_clean.csv"

> list.files(path = 'example', pattern = 'abc*clean')
character(0)

> list.files(path = 'example', pattern = '[abc][clean]')
[1] "abc_clean.csv"           "abc_other_clean.csv"     "abc_other_raw.csv"      
[4] "abc_raw.csv"             "abc_something_clean.csv" "abc_something_raw.csv"  
[7] "def_clean.csv"           "def_other_clean.csv"     "def_something_clean.csv"

For this example, the result I would like is something like:

[1] "abc_clean.csv"           "abc_other_clean.csv"     "abc_something_clean.csv"

And please note that this is not about the specific case of listing files. It's not the first time I've tried to get an and on a regex and got confused.

data

x <- c("abc_clean.csv", "abc_other_clean.csv", "abc_other_raw.csv", "abc_raw.csv",
       "abc_something_clean.csv", "abc_something_raw.csv", "def_clean.csv",
       "def_other_clean.csv", "def_other_raw.csv", "def_raw.csv", "def_something_clean.csv",
       "def_something_raw.csv")
markus
  • 25,843
  • 5
  • 39
  • 58
Leo Barlach
  • 480
  • 3
  • 13
  • Related / dupe [Regular Expressions: Is there an AND operator?](https://stackoverflow.com/questions/469913/regular-expressions-is-there-an-and-operator) – markus Mar 22 '19 at 17:53

2 Answers2

1

Why not just allow any character between? abc.*clean.*

If you need expect different order, how about use OR: abc.*clean.*|clean.*abc.*

Demo

TheWandererLee
  • 1,012
  • 5
  • 14
0

Try Regex: ^(?=.*abc)(?=.*clean).*$

Demo

Matt.G
  • 3,586
  • 2
  • 10
  • 23
  • 1
    Since the `list.files` function doesn't seem to support perl, they'd probably do as seen here with the R code around the pattern: https://stackoverflow.com/a/31590908 – Frank Mar 22 '19 at 17:39
  • 1
    Doesn't seem to work on R: `> list.files(path = 'example', pattern = regex('^(?=.*abc)(?=.*clean).*$')) Error in list.files(path = "example", pattern = regex("^(?=.*abc)(?=.*clean).*$")) : invalid 'pattern' regular expression` – Leo Barlach Mar 22 '19 at 17:43