pattern = r'[ -\\[\\]]'
regex = re.compile(pattern)
name = '123[ shiv'
new_name = regex.sub('_',name)
gives result(new_name) ::
'_____shiv'
instead of::
'123__shiv'
..thanx in advance
Your regex is creating a range from whitespace
(ASCII Code - 32) to opening bracket - [
(ASCII Code - 91) because of that -
in between. And that range includes the numbers 0 to 9
(ASCII Code - 48 to 57).
You need to change your regex to: -
pattern = '[- \\[\\]]'
Moved -
at the beginning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With