AT_1965
AT_1965

Reputation: 107

Substituting strings with sub() method with regex

I didn't want to write and ask this forum but I am stuck and the book I'm following, that supposed to be for beginners, is anything but...

Anyway... In the string below:

'Agent Alice told Agent Bob that Agent Steve was a double agent.'

I want to show just the first letter of the agent's first name. So what I end up with, is:

'Agent A**** told Agent B**** that Agent S**** was a double agent.'

I tried using grouping, like in the book, but its not working.

namesRegex = re.compile(r'\w?([A-Z])')
mo = namesRegex.sub(r'\1****', 'Agent Alice told Agent Bob that Agent 
Steve was a double agent.')
print(mo)

Also, I would welcome any recommended additional resources on this topic Thanks in advance...

Upvotes: 0

Views: 119

Answers (1)

akuiper
akuiper

Reputation: 214957

You can use look behind ?<= syntax for this:

namesRegex = re.compile(r'(?<=Agent\s[A-Z])\w+')
mo = namesRegex.sub(r'****', 'Agent Alice told Agent Bob that Agent Steve was a double agent.')

mo
# 'Agent A**** told Agent B**** that Agent S**** was a double agent.'

This will replace any word character \w+ including alpha numeric characters and underscore _ after the pattern Agent\s[A-Z] with ****. If it's not guaranteed the agent's names starts with capital letter Agent\s[A-Za-z] would be a less restricted option.

Upvotes: 1

Related Questions