-2

I would like to know how to replace an exact word BUT with the exception of ignoring punctuation

I've see this answer which is nice but fails on the edges for me.

Here is an example to what I would like to replace:

"hi my name is John, and I am 30 yrs old."

to

"to hi my name is ted, and I am 30 yrs old."

Thanks

Community
  • 1
  • 1
Ofer Helman
  • 714
  • 1
  • 8
  • 25
  • 2
    Dd you try anything yourself? – moffeltje Jun 04 '15 at 11:48
  • Sure, but I am not too good at regex – Ofer Helman Jun 04 '15 at 11:51
  • 1
    I'm confused. Does "ignoring punctuation" mean "removing punctuation from the end product", or "not doing anything special with existing punctuation, leaving it where it is"? If it's the former, why does your output still have punctuation? And if it's the latter, why did you mention it at all, and why did you link to a question about removing punctuation? – Kevin Jun 04 '15 at 11:55
  • Yeah, no idea why punctuation is even being mentioned here. Do you just want to replace "John" with "Ted" in a string? – Rob Grant Jun 04 '15 at 12:15

2 Answers2

2
>>> s = "hi my name is John, and I am 30 yrs old."
>>> new_s = s.replace("John", "Ted")
>>> new_s
'hi my name is Ted, and I am 30 yrs old.'
Kevin
  • 74,910
  • 12
  • 133
  • 166
1
\bJohn\b

This should do it for you.Replace by ted.

vks
  • 67,027
  • 10
  • 91
  • 124