Match space between chars except start of line

Viewed 40

I have a substitution in Python which looks like this :

re.sub('','?',"Man")

The only problem is that the output is :

?M?a?n?

But I want to avoid the first substitution so it looks like this :

M?a?n?

How can I avoid matching only the start of the line but keep matching everything else?

2 Answers

If you must use regex, you could use a negative look-ahead assertion:

re.sub(r'(?!^)', '?', "Man")
# Yields "M?a?n?"

If you are okay not using regex. Not exactly the way you want but will still do the job

'?'.join("Man")+'?'
Related