Using grepl in R to search for an asterisk

前端 未结 1 647
借酒劲吻你
借酒劲吻你 2020-12-12 02:21

I am reading a phrase into an R script as an argument. If the phrase contains an asterisk (*), I do not want the script to run.

However, I am having is

1条回答
  •  轮回少年
    2020-12-12 03:06

    Try this:

    p <- c("Hello", "H*llo")
    grepl("\\*", p)
    
    [1] FALSE  TRUE
    

    This works because the * asterisk has special meaning in a regular expresssion. Specifically, * means find zero or more of the previous element.

    Thus you have to escape the asterisk using \\*. The double escape is necessary because the \ already has the meaning of escape in R.

    0 讨论(0)
提交回复
热议问题