I'm having a trouble with this regular expression. Consider the following vector.
> vec <- c("new jersey", "south dakota", "virginia:chincoteague",
"washington:whidbey island", "new york:main")
Of those strings that contain a :
, I would like to keep only the ones with main
after :
, resulting in
[1] "new jersey" "south dakota" "new york:main"
So far, I've only been able to get there with this ugly nested nightmare, which is quite obviously far from optimal.
> g1 <- grep(":", vec)
> vec[ -g1[grep("main", grep(":", vec, value = TRUE), invert = TRUE)] ]
# [1] "new jersey" "south dakota" "new york:main"
How can I write a single regular expression to keep :main
but remove others containing :
?
Using |
(Pick one that contains :main
or that does not contains :
at all):
> vec <- c("new jersey", "south dakota", "virginia:chincoteague",
+ "washington:whidbey island", "new york:main")
> grep(":main|^[^:]*$", vec)
[1] 1 2 5
> vec[grep(":main|^[^:]*$", vec)]
[1] "new jersey" "south dakota" "new york:main"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With