Python replace a string using re.sub only if prefix and suffix matches

Sachin Shetty

I am trying to convert German words to English using custom dictionary. In below code,replace should only happen if the suffix or prefix of the matching word falls in characters

[,\/!?()_1234567890-=+."""' "]

For exampple: Mein should be converted at first but not in MeinName as the prefix and suffix are not characters mentioned above. If there were single word like _Mein or Mein. it need be converted.

import re

str = "Mein ,Name, ist John, Wo23 bist+ ,_du? , MeinName "
replacements = { 'Mein':'my', 'ist':'is', 'Wo':'where', 'bist':'are', 'du':'you', 'is':'iis'}
re.sub('({})'.format('|'.join(map(re.escape, replacements.keys()))), lambda m: replacements[m.group()], str)

Expected output :

my ,name,is John,where23 are+,_you? ,MeinName 
Wiktor Stribiżew

You can use

import re
s = "Mein ,Name, ist John, Wo23 bist+ ,_du? , MeinName "
replacements = { "Mein": "my", "ist": "is", "Wo":"where", "bist":"are", "du":"you", "is" :"iis"}
rx = r'(?:{})(?=[,/!?()_0-9\-=+."\s\'])'.format('|'.join(map(re.escape, replacements.keys())))
print (rx)
print ( re.sub(rx, lambda m: replacements[m.group()], s) )
# => my ,Name, is John, where23 are+ ,_you? , MeinName 

See the Python demo.

The regex will look like

(?:Mein|ist|Wo|bist|du|is)(?=[,/!?()_0-9\-=+."\s\'])

See the regex demo. Details:

  • (?:Mein|ist|Wo|bist|du|is) - one of the alternative strings
  • (?=[,/!?()_0-9\-=+."\s\']) - a positive lookahead matching a location that is immediately followed with ,, /, !, ?, ), (, _, a digit, -, =, +, ., ", whitespace and '.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python re.sub, only replace part of match

Use Python's string.replace vs re.sub

How to replace only part of the match with python re.sub

jQuery replace a subdirectory with prefix and suffix

Replace "&&" with "and" using re.sub in python.

String replacements using re.sub in python

python re.sub replace number in match string

Python re.sub(): trying to replace escaped characters only

Replace a digit in a sentence using re.sub() in python

Using re.sub() in python how to replace certain phrase but only if there is more than one letter ahead of it

replace only matches the beginning of the string

Using re.sub() in python to replace html code

Replace prefix and suffix using batch

Replace a string without certain prefix and suffix in Java

how to conditionally replace prefix or prefix/suffix gsub

How to add prefix and suffix to a string in python

How to replace string using RE group in python?

Using re.sub() to replace some part of a string

re.sub to replace multiple lists with corresponding matches (Python)

Using re.sub with capture groups to replace only portion of a match

Python - replace prefix with other substring and add it as suffix

replace before and after a string using re in python

Python3 re.sub only replace once, how to replace all?

Using difflib.get_close_matches to replace word in string - Python

Using python re.sub, but it replace the start and end unexpected

How to replace a word suffix using re.sub() in Python?

string to replace character only it matches symbols using regex

Python Regular Expression: re.sub to replace matches

What is the difference between re.sub and String replace in Python 3?