我正在尝试这样的事情:
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
答案 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"