Re: [R] grep pattern

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Fri, 20 May 2011 19:09:24 -0400

On May 20, 2011, at 11:57 AM, Kang Min wrote:

> Hi all,
>
> I'm trying to subset a pattern in a vector. Each argument has 6
> letters, and I need those that start with Z and end with Z.
>
> e.g.
> x <- c("ZFHSJK", "ZFHJKZ","ZIOPWE","ZLKJSD","ZKFLPZ")
>
> I've looked up other discussions but still can't seem to find the
> answer.

You may need to study the regex page a bit longer

the "^" is the beginning of a string
".+" will math can arbitrarily long string of anything and "$" indicates the end of a string

 > x <- c("ZFHSJK", "ZFHJKZ","ZIOPWE","ZLKJSD","ZKFLPZ")  > grep("^Z.+Z$", x)
[1] 2 5
 > grep("^Z.+Z$", x, value=TRUE)
[1] "ZFHJKZ" "ZKFLPZ"
>
> Thanks.
> Kangmin
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 20 May 2011 - 23:13:08 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 22 May 2011 - 22:10:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive