正则表达式的字符为or
,但and
的字符是什么?
例如,假设我有一个包含以下文件的文件夹:
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"
我想匹配同时具有“ abc”和“ clean”的文件。这些都不起作用:
> 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"
对于这个例子,我想要的结果是这样的:
[1] "abc_clean.csv" "abc_other_clean.csv" "abc_something_clean.csv"
请注意,这与列表文件的特定情况无关。这不是我第一次尝试在正则表达式上使用and
并感到困惑。
数据
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")