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")