When replacing a character string with multiple patterns, it tends to be redundant code like ↓.
#For example
# " <-> '
# abc...z -> *
# ABC...Z -> AA, BB, CC, ...,ZZ
#If you want to make a replacement like
text = "'abc'" + '"ABC"'
#pattern 1
replaced_text = text.replace('"', '#').replace("'", '"').replace('#', "'").replace("a", "*"). .......
#Pattern 2
trdict = str.maketrans({'"': "'", "'": '"', "a": "*", "b": "*", .......})
replaces_text = text.translate(trdict)
#Pattern 3
import re
replaced_text = re.sub("#", '"', re.sub('"', "'", re.sub("'", '#', re.sub("[a-z], "*", re.sub("[A-Z]", "\\1\\1", text)))))
#etc...
In addition, in the case of replacement methods such as patterns 1 and 3, replacement is performed in order, so it is necessary to consider the possibility of unexpected replacement such as further replacement of characters after replacement. .. However, if you can't use regular expressions like pattern 3, it will take a lot of time and effort.
To eliminate such dissatisfaction
** You can also use regular expressions, You can pass the replacement patterns together in a dictionary, All replacements can be done at the same time **
I wrote a function.
import re
from typing import Dict
def replaces(text: str, trdict: Dict[str, str]) -> str:
"""
IN:
Source text
Replacement dictionary
OUT:
Replaced text
NOTE:
You can use regular expressions.
If more than one pattern is matched,
the pattern closest to the front of the dictionary takes precedence.
EXAMPLE:
text = "'abc'" + '"ABC"'
replaces(text, {"'": '"', '"': "'", "[a-z]": "*", "([A-Z])": "\\1\\1"})
---> "***"'AABBCC'
"""
return re.sub(
"|".join(trdict.keys()), lambda m: next(
(re.sub(pattern, trdict[pattern], m.group(0)) for pattern in trdict
if re.fullmatch(pattern, m.group(0)))), text)
First argument: Original string
Second argument: Replacement dictionary {before: after}
Return value: Character string after replacement
text = "'abc'" + '"ABC"'
trdict = {"'": '"', '"': "'", "[a-z]": "*", "([A-Z])": "\\1\\1"}
replaces(text, trdict)
# ---> "***"'AABBCC'
If multiple patterns in the dictionary are matched, the previous pattern takes precedence.
Recommended Posts