After scratching my head and extensive googling, I can't seem to get this right.
I have this sample string:
test = "true sales are expected to be between 50% and 60% higher than those reported for the previous corresponding year. the main reason is blah blah. the fake sales are expected to be in the region of between 25% and 35% lower."
I'm trying to determine whether the 'true' sales where higher or lower. Using R
, and the 'stringr' library, I'm trying it as follows:
test = "true sales are expected to be between 50% and 60% higher than those reported for the previous corresponding year. the main reason is blah blah. the fake sales are expected to be in the region of between 25% and 35% lower."
positive.regex = "(sales).*?[0-9]{1,3}% higher"
negative.regex = "(sales).*?[0-9]{1,3}% lower"
Which yields the following results:
str_extract(test,positive.regex) [1] "sales are expected to be between 50% and 60% higher" str_extract(test,negative.regex) [1] "sales are expected to be between 50% and 60% higher than those reported for the previous corresponding year. the main reason is blah blah. the fake sales are expected to be in the region of between 25% and 35% lower"
I'm trying to find a way to limit the number of words matched between (sales) and '% higher'
or '% lower'
, so that the negative regex won't match. i.e I know I need to replace '.*?' with something that matches whole words, not characters, and limit the number of these words to something like 3-5, how can I do this?