I have a string of variable names and I want to extract the currencies as given by a vector from that. But I am having difficulties extracting the values.
My first approach was to replace all but the currencies abbreviations with nothing.
For example:
x <- c("Total Assets in th USD", "Equity in mil EUR", "Number of Branches")
currencies <- c("USD", "EUR", "GBP")
regex <- paste0("([^",
paste(currencies, collapse = "|"),
"])")
# results in
# "([^USD|EUR|GBP])"
gsub(regex, "", x)
# [1] "USD" "EEUR" "B"
The expected result would be c("USD", "EUR", "")
This is obviously wrong, as it matches the individual characters (E, U, R) instead of the character group (EUR). Now my question is, how can I extract only the given groups?