August Edwards
August Edwards

Reputation: 101

Matching a string based on specific subparts

Imagine I am given some complicated string

string_complicated = "asfdalksdVARIABLE1/djdj/.VARIABLE2?jjjVARIABLE3yuuu" 

which contains the variables VARIABLE1, VARIABLE2, VARIABLE3.

Now consider the simpler string:

string_simpler = "VARIABLE1,VARIABLE2,VARIABLE3" 

Is there a simple way to match string_complicated with string_simpler. I.e. in pseudocode I am looking for something like:

match(string_complicated, string_simpler) = TRUE 

But ONLY if all variables are present. I.e. I would like the matching procedure to output the following for the example below:

match(string_complicated, "VARIABLE1,VARIABLE2") = FALSE

Thanks in advance

I have tried various version of gsub without luck.

Upvotes: 0

Views: 20

Answers (1)

yuk
yuk

Reputation: 19880

Split the string_simpler to a vector of individual variables, then check string_complicated against each one.

string_complicated = "asfdalksdVARIABLE1/djdj/.VARIABLE2?jjjVARIABLE3yuuu" 
string_simpler = "VARIABLE1,VARIABLE2,VARIABLE3" 
string_list = strsplit(string_simpler, ",")[[1]]
all(stringr::str_detect(string_complicated, string_list))
#> [1] TRUE

Created on 2024-12-20 with reprex v2.0.2

Upvotes: 0

Related Questions