0

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...

2
  • It's not working. Do you get an error or? Commented Sep 9, 2016 at 22:54
  • i get this: ***gent ****lice told ****gent ****ob that ****gent ****teve was a double agent. Commented Sep 9, 2016 at 23:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. yes that worked. So ?<= is after the pattern? That was not in the book.
You can check here regular-expressions.info/lookaround.html, it is a look behind syntax which means only replace words after the pattern specified in (?<=...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.