R use variable within regex

前端 未结 3 1309
孤城傲影
孤城傲影 2021-01-16 02:27

Okay - maybe this is a better example. I am looking for guidance/references on how to reference a variable within a regex - not how to build a regex for this data.

3条回答
  •  情深已故
    2021-01-16 02:54

    Try using the paste0() function. That will put together all your variables and any regular expressions you want to use.

    grep(paste0("^.*", variable, ".*$"), d1)
    

    you can also add the argument collapse = "" to paste0() if your variable could have >1 element

提交回复
热议问题