[R] Match beginning and end of string (grepl)

Johannes Radinger johannesradinger at gmail.com
Tue Sep 2 14:12:19 CEST 2014


Hi,

I'd like to match the beginning and the end of a string. E.g. I want to
extract all strings from a vector that beginn with "12" and end with
"Apples":

a <- "2 green Apples"
b <- "12 green Apples"
c <- "12 Apples and 2 green Bananas"
d <- "12 yellow Bananas"

fruitlist <- c(a,b,c,d)

# This is how to extract all that beginn with 12
grepl("^12",fruitlist)

But how can I get only those that also end with "Apples". So basically
just item b "12 green Apples" should remain.

Is there any clear description and examples of regular expressions
and how to use them? I find the manual ?grepl very difficult to read.

Thanks,
Johannes

	[[alternative HTML version deleted]]



More information about the R-help mailing list