Home > other >  How to move 'n' only if it precedes a capital letter?
How to move 'n' only if it precedes a capital letter?

Time:03-29

text = [nDog, nCat, cat, Pizza, nPizza, nFootball, football, peter, nPeter, nNumber, neuron, naughty, nNaughty]

I want to remove the n's that precede only the character. If I try:

re.sub(r"n"," ",text)

All of the n's get removed:

text = [Dog, Cat, cat, Pizza, Pizza, Football, football, peter, Peter, Number, euron, aughty, Naughty]

How can I solve this?

Thanks

CodePudding user response:

A lookahead assertion should do the trick.

re.sub(r"n(?=[A-Z])","",text)
  • Related