Skip to content

grep

5 messages · Jeff Newmiller, Andrew Simmons, Steven Yen

#
Dear, Below, jj contains character strings starting with ?z.? and ?x.?. 
I want to grep all that contain either ?z.? or ?x.?. I had to grep ?z.? 
and ?x.? separately and then tack the result together. Is there a 
convenient grep option that would grep strings with either ?z.? or ?x.?. 
Thank you!

 > jj<-names(v$est); jj
 ?[1] "z.one"???? "z.liberal" "z.conserv" "z.dem"???? "z.rep" "z.realinc"
 ?[7] "x.one"???? "x.liberal" "x.conserv" "x.dem"???? "x.rep" "x.realinc"
[13] "mu1_1"???? "mu2_1"???? "rho"
 > j1<-grep("z.",jj,value=TRUE); j1
[1] "z.one"???? "z.liberal" "z.conserv" "z.dem"???? "z.rep" "z.realinc"
 > j2<-grep("x.",jj,value=TRUE); j2
[1] "x.one"???? "x.liberal" "x.conserv" "x.dem"???? "x.rep" "x.realinc"
 > j<-c(j1,j2); j
 ?[1] "z.one"???? "z.liberal" "z.conserv" "z.dem"???? "z.rep" "z.realinc"
 ?[7] "x.one"???? "x.liberal" "x.conserv" "x.dem"???? "x.rep" "x.realinc"
#
grep( "^(z|x)\\.", jj, value = TRUE )

or

grep( r"(^(z|x)\.)", jj, value = TRUE )
On July 10, 2022 6:08:45 PM PDT, "Steven T. Yen" <styen at ntu.edu.tw> wrote:

  
    
#
Thanks Jeff. It works. If there is a good reference I should read 
(besides ? grep) I's be glad to have it.
On 7/11/2022 9:30 AM, Jeff Newmiller wrote:
#
?regex is a nice starting point, it's got plenty of details on meta
characters and characters classes, if you need more advanced stuff you'll
probably have to look at the perl regex documentation, I believe it's
linked in ?regex.

I might try something like

grep("^[xz]\\.")

or

grep("^[xz][.]")

or if you'd consider a different function,

startsWith(jj, "x.") | startsWith(jj, "z.")
On Sun, Jul 10, 2022, 21:49 Steven T. Yen <styen at ntu.edu.tw> wrote:

            

  
  
#
Thank you all.
On 7/11/2022 10:32 AM, Andrew Simmons wrote: