I need to replace some things in a string using an array, they can look like this:
array = [3, "$x" , "$y", "$hi_buddy"]
#the first number is number of things in array
string = "$xena is here $x and $y."
I've got another array with things to replace those things, let's say its called rep_array.
rep_array = [3, "A", "B", "C"]
For the replacement I use this:
for x in range (1, array[0] + 1):
string = string.replace(array[x], rep_array[x])
But the result is:
string = "Aena is here A and B."
But I need to much only lonely $x not $x in another word. Result should look like this:
string = "$xena is here A and B."
Note that:
- all patterns in
array
start with$
. - a pattern matches if it matches the whole word after
$
;$xena
doesn't match$x
, butfoo$x
would match. $
can be escaped with@
and than it should not be matched (for example$x
does not match@$x
)