Berry Boessenkool
Berry Boessenkool

Reputation: 1538

R: string ends with one of strings

Is there a more elegant way to find out which elements of a character string vector end with a colon, period or bracket? (Also, I might later want to include question marks etc.)

x <- c("2)", "This", "is", "a", "Test:", "Find", "the", "dots.", "Which", "ones")
which(grepl("\\.$", x) | grepl("\\:$", x) | grepl("\\)$", x))

Upvotes: 0

Views: 842

Answers (1)

Berry Boessenkool
Berry Boessenkool

Reputation: 1538

Thanks, nrussell and docendo discimus.

grep("[.:)]$", x)

Upvotes: 2

Related Questions