Use variable as key word for grep function

Viewed 36

I have successfully used this syntax to assign and locate keywords. I'd like to take the same approach but instead of knowing the word in advance like in the example below, I want to pass the word/expression as a variable, for example replacing options, with a var. How would I do that?

phrase <- "(options) ([^ ]+)"

1 Answers

You can use paste0:

phrase <- paste0("(", optionsVar, ") ([^ ]+)")

Also, note that [^ ]+ matches one or more chars other than spaces, but you can probably replace it with \\S+, one or more non-whitespace chars. Same with the literal space: \\s or \\s+ might prove more flexible.

Related