[R] grep for asterisks "*"'s

Tony Breyal tony.breyal at googlemail.com
Sun Nov 23 10:33:56 CET 2008


Hi Robin!

Does the following produce what you want mate?

### R START ###
> text <- 'this sentence contains an * (i.e. an astrix).'
> grep('\\*', text)
[1] 1
### R END ###

Regards,
Tony Breyal

On 23 Nov, 08:41, Robin Clark <robalexcl... at hotmail.com> 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:
>
> 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-tp20644195p20644...
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list