0

Background

I have the following code that is a variation of the #Replace a name in a string in Python

s = "The myth of Johnny Appleseed states that he ate many apple seeds"
s.replace('Johnny Appleseed','***')

Output

'The myth of *** states that he ate many apple seeds'

Problem

But what if the first and last name contains many variations (non-exhaustive list)

#misspellings 
Johnny Applseed
Johny Appleseed
Johnny Applesee

#Letter case 
johnny appleseed
JOHNNY APPLESEED
JoHny ApPleseED

Question

How do I get the same output as above

'The myth of *** states that he ate many apple seeds'

If any or all of these variations are present in the first and last name?

SFC
  • 733
  • 2
  • 11
  • 22
  • In that case you could account for the characters by making some optional using `?` or use quantifier like `*` for 0+ times or `+` for 1+ times and make the match case insensitive by perhaps using a flag `/i` or set it in the code if possible. For example `\bJohn+y Apple?seed?\b` see https://regex101.com/r/FaJqv6/1 – The fourth bird Jun 24 '19 at 20:21

1 Answers1

0

Good question!

If we would know that'd be Johnny, then we would start with an expression similar to:

\b[johny]+\b\s+\b[aplsed]+\b

If not, we would design a close expression, then we would trade off for the accuracy that we might want.

Please see the demo for additional info and explanation in here.

Emma
  • 27,428
  • 11
  • 44
  • 69