[英]Python replace a string using re.sub only if prefix and suffix matches
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.例如:
Mein
应该先转换, MeinName
中不需要转换,因为前缀和后缀不是上面提到的字符。 If there were single word like _Mein
or Mein.
如果有像
_Mein
或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:预计 output:
my ,name,is John,where23 are+,_you? ,MeinName
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 .请参阅Python 演示。
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 (?:Mein|ist|Wo|bist|du|is)
- 备选字符串之一(?=[,/?.()_0-9\-=+."\s\'])
- a positive lookahead matching a location that is immediately followed with ,
, /
, !
, ?
, )
, (
, _
, a digit, -
, =
, +
, .
, "
, whitespace and '
. (?=[,/?.()_0-9\-=+."\s\'])
- 与紧随其后的位置匹配的正前瞻,
, /
, !
, ?
, )
, (
, _
, 数字, -
, =
, +
, .
, "
, 空格和'
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.