lets say I have the following string in Python:
s = "Hi, I currently have 2 apples and 3 oranges"
in regex we can do
r = re.findall(r'\d',s)
and this would give us a list containing the numbers:
["2","3"]
However, lets say I want to substitute these numbers using a list in the order that they appear in the sentence.
new_list = ["4","5"]
and make the new sentences say:
"Hi, I currently have 4 apples and 5 oranges"
I tried doing the following:
new_sentence = [re.sub(('\d'),x, s) for x in new_list]
But that gave me:
['Hi, I currently have 4 apples and 4 oranges', 'Hi, I currently have 5 apples and 5 oranges']
Which is not what I wanted. How do you substitute values using regex in the order that they appear using a list?