Background: Absolute beginner working through the book "Automate the Boring Stuff with Python" and was curious as to how to make this substitution:
Input string:
"Agent Alice tells Agent Bob secret info about Agent Charlie."
Desired Output:
"Agent A**** tells Agent B** secret info about Agent C******."
Note how the number of asterisks depends on the length of the name.
What I've tried:
import re
concealName = re.compile(r'(Agent)(\s)(\w)(\w)*')
sentence = "Agent Alice tells Agent Bob secret info about Agent Charlie."
concealName.sub(r'\1\2\3' + '*' * len(r'\4'), sentence)
OUTPUT:
'Agent A** gave Agent B** info about Agent C**'
My logic was that it would calculate the length of each instance of a matched group 4, and multiply that length with the '*' string, which should've resulted in a string of ****** with the same length as the group 4 matched. Completely clueless as to why this didn't work, and how to fix it.