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:
array
start with $
.$
; $xena
doesn't match $x
, but foo$x
would match.$
can be escaped with @
and than it should not be matched (for example $x
does not match @$x
)Use a regular expression that wraps your source text with some whitespace look-behind and a \b
anchor; make sure to include the start of the string too:
import re
for pattern, replacement in zip(array[1:], rep_array[1:]):
pattern = r'{}\b'.format(re.escape(pattern))
string = re.sub(pattern, replacement, string)
This uses re.escape()
to ensure any regular expression meta characters in the pattern are escaped first. zip()
is used to pair up your patterns and replacement values; a more pythonic alternative to your range()
loop.
\b
only matches at a position where a word character is followed by a non-word character (or vice versa), a word boundary. Your patterns all end in a word character, so this makes sure your patterns only match if the next character is not a word character, blocking $x
from matching inside $xena
.
Demo:
>>> import re
>>> array = [3, "$x" , "$y", "$hi_buddy"]
>>> rep_array = [3, "A", "B", "C"]
>>> string = "$xena is here $x and $y. foo$x matches too!"
>>> for pattern, replacement in zip(array[1:], rep_array[1:]):
... pattern = r'{}\b'.format(re.escape(pattern))
... string = re.sub(pattern, replacement, string)
...
>>> print string
$xena is here A and B. fooA matches too!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments