[R] grep for asterisks "*"'s

(Ted Harding) Ted.Harding at manchester.ac.uk
Sun Nov 23 10:29:07 CET 2008


On 23-Nov-08 08:41:02, Robin Clark wrote:
> Hello,
> I'm trying to determine if a string contains asterisks using the grep
> function. I know that this is slightly difficult because * is a special
> character in regular expressions and needs to be escaped. However,
> escaping the * using \ doesn't work either:

In a regular expression, a term like [*] will match "*" literally,
even though "*" is normally a special ("meta") character. Thus:

  test<-c("1*1","222","*33","44*","555")
  test
# [1] "1*1" "222" "*33" "44*" "555"
  ?grep
  grep("[*]",test)
# [1] 1 3 4
  test[grep("[*]",test)]
# [1] "1*1" "*33" "44*"

This also works for other metacharacters, even (surprise!) "]":
  test2<-c("1[1","222","[33","44]","55]")
  grep("[]]",test2)
# [1] 4 5
  grep("[[]",test2)
# [1] 1 3


However, your error was to use "\*" with only one "\".
You should have used "\\*":

  grep("\\*",test)
# [1] 1 3 4

and the "\\" also works for other special characters, though "\"
itself can be tricky since to enter a "\" into a string you have
to type it as "\\", and R will print it back as "\\" as well:

  test3<-c("1\\1","222","\\33","44\\","55\\")
  test3
# [1] "1\\1" "222"  "\\33" "44\\" "55\\"
  nchar(test3)
# [1] 3 3 3 3 3

so there really are only 3 characters in each element of test3.
But to grep for "\" you have to enter "\\" twice over:

  grep("\\",test3)
# Error in grep(pattern, x, ignore.case, extended, value, fixed) : 
#         invalid regular expression
  grep("\\\",test3)
# Error: syntax error
  grep("\\\\",test3)
# [1] 1 3 4 5

And, just as [*] catches "*", so "[\\]" is needed to catch "\";

  grep("[\\]",test3)
# [1] 1 3 4 5

Hoping this helps!
Ted.

> if(grep("\*", model)>0) #does the model have an interaction
> {
>    do something...
> }
> 
> produces the following error message:
> 
> Error in grep("*", model) : invalid regular expression '*'
> In addition: Warning messages:
> 1: '\*' is an unrecognized escape in a character string 
> 2: unrecognized escape removed from "\*" 
> 3: In grep("*", model) :
>   regcomp error:  'Invalid preceding regular expression'
> Execution halted
> 
> Any ideas anyone?
> Robin
> -- 
> View this message in context:
> http://www.nabble.com/grep-for-asterisks-%22*%22%27s-tp20644195p20644195
> .html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 23-Nov-08                                       Time: 09:29:03
------------------------------ XFMail ------------------------------



More information about the R-help mailing list