Python replace, using patterns in array

user3394087

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, but foo$x would match.
  • $ can be escaped with @ and than it should not be matched (for example $x does not match @$x)
Martijn Pieters

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read a flat text file and replace specific list of patterns using python

From Dev

Replace patterns using gsub and blocks

From Dev

How to replace multiple patterns with given array?

From Dev

Using sed to replace characters between two patterns

From Dev

Rename-Item using -Replace with multiple patterns

From Dev

How can I replace patterns using re module in python3

From Dev

Python replace multiple string patterns in column

From Dev

Using recursion to draw patterns in python

From Dev

Using recursion to draw patterns in python

From Dev

preg_match - using an array of patterns - PHP

From Dev

XSLT - replace text node by identifying patterns using regex

From Dev

XSLT - replace text node by identifying patterns using regex

From Dev

Using a dictionary as a source of Regex.Replace patterns in a Linq statement

From Dev

JQuery: replace array value using .replace()

From Dev

search and replace using python

From Java

Finding similar patterns in text using Python 3.7

From Dev

How to create patterns in Python using nested loops?

From Dev

Python grouping similar patterns using regex

From Dev

Python grouping similar patterns using regex

From Dev

Using preg_replace on an array

From Dev

How to search for an array of patterns in another array using Google Script?

From Dev

Replace n patterns in a string

From Dev

Replace n patterns in a string

From Dev

Replace patterns in a string

From Dev

search and replace string with patterns

From Dev

Python: Replace a number in array with a string

From Dev

Python: Replace a number in array with a string

From Dev

Using -replace when selecting object to replace using an $array

From Dev

Python Patterns

Related Related

HotTag

Archive