寻找合适的grep函数模式参数语法

时间:2014-10-23 07:47:26

标签: regex r grep

我正在尝试这样的事情:

animal_stories <- c("dogs are loyal", "cats are moody",
                    "cat was chasing mouse who was chased by dog",
                    "aabjjada adda", "dont play cat and mouse game", "ssss", 
                    "dog and cats are never friends", "jduh hisdsaug")  

grep("cat AND mouse|dog", animal_stories, ignore.case=T)

并期望输出类似于:

## [1] cat was chasing mouse who was chased by dog
## [2] dog and cats are never friends
## [3] dont play cat and mouse game

1 个答案:

答案 0 :(得分:0)

此正则表达式匹配(a)"cat"后跟"dog""mouse"和(b)"dog""mouse"后跟"cat"

grep("(dog|mouse).*cat|cat.*(dog|mouse)", animal_stories, ignore.case = TRUE, value = TRUE)
# [1] "cat was chasing mouse who was chased by dog"
# [2] "dont play cat and mouse game"               
# [3] "dog and cats are never friends"   
相关问题