Title may be confusing, so allow me to elaborate, say I have initialized the following strings
some_string_1 <- "Charges: $400.50 applied to account 1, $300.70 applied to account 2, $120.30 applied to account 3"
some_string_2 <- "Charges: $500.50 applied to account 2, $200.10 applied to account 3, $150.90 applied to account 1"
Lets say I am only focusing on account 1, so I want to extract to numbers after the dollar sign associated with the charge on account 1. I want to know how, using regex and functions like str_extract, I can get the following outputs:
Suppose I call str_extract(some_string_1, regexp), I would like it to give
[1] "400.50"
And suppose I call str_extract(some_string_2, regexp), I would like it to give
[1] "150.90"
As you can see, the charge applied to account 1 is in different places in both strings (it is the first charge listed in string 1 but the third one in string 2), so the regular expression has to account for this.
So far all I have is the following: regexp <- "(?<=\\$)\\d+(?=.)", which successfully gives me the number after the first dollar sign that appears in the string (without the digits in the decimal place that is), but I have no idea how to specify that I only want the number after the dollar sign that is associated with the charge applied on account 1. Looking for help on this aspect.