正则表达式与此模式和那个模式

时间:2019-03-22 17:33:04

标签: r regex

正则表达式的字符为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")

2 个答案:

答案 0 :(得分:1)

为什么不只允许它们之间的任何字符? abc.*clean.*

如果您需要不同的订单,请使用OR:abc.*clean.*|clean.*abc.*

Demo

答案 1 :(得分:0)

尝试使用正则表达式:^(?=.*abc)(?=.*clean).*$

Demo