Home > OS >  Use variable as key word for grep function
Use variable as key word for grep function

Time:10-08

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) ([^ ] )"

CodePudding user response:

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