0

The idea is to replace the whole word (+)-glutamic: with "(+)-glutamic":. However escaping special characters as shown in the snippet doesn't seem to work.

If the special characters are not at the beginning of the string, then it works okay. I'm using \b to make sure only full words are matched, i.e. it should not match the substring in glutamic acid: {}.

Is there something I'm missing?

import re

pathways = "(+)-glutamic: {} glutamic acid: {}"

compound = "(+)-glutamic"

m = re.sub(r"\b{}:".format(re.escape(compound)), f'"{compound}":', pathways)

print(m)
vendrediSurMer
  • 361
  • 3
  • 15
  • See [the demo](https://ideone.com/2QNG6S) and [another post](https://stackoverflow.com/a/45145800/3832970). Also, more details are available in [this YT video](https://www.youtube.com/watch?v=ngbxagE2b68). – Wiktor Stribiżew Apr 26 '22 at 17:12

0 Answers0