I have a string s
where "substrings" are divided by a pipe. Substrings might or might not contain numbers. And I have a test character string n
that contains a number and might or might not contain letters. See example below. Note that spacing can be any
I'm trying to drop all substrings where n
is not in a range or is not an exact match. I understand that I need to split by -
, convert to numbers, and compare low/high to n
converted to numeric. Here's my starting point, but then I got stuck with getting the final good string out of unl_new
.
s = "liquid & bar soap 1.0 - 2.0oz | bar 2- 5.0 oz | liquid soap 1-2oz | dish 1.5oz"
n = "1.5oz"
unl = unlist(strsplit(s,"\\|"))
unl_new = (strsplit(unl,"-"))
unl_new = unlist(gsub("[a-zA-Z]","",unl_new))
Desired output:
"liquid & bar soap 1.0 - 2.0oz | liquid soap 1-2oz | dish 1.5oz"
Am I completely on the wrong path? Thanks!