1

I was implementing a simple script that should replace unwanted characters form a string. This is what I got in iPython:

>>> import re
>>> def clean(value):
...     ret = re.sub(r"^[A-Za-z0-9]", "", value)
...     return ret
... 
>>> clean("What")
'hat'
>>>

I was sure my python instalation was broken or something, but I get the same on https://www.python.org/shell/. What is going on?

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
de_dux
  • 21
  • 1
  • 5
  • 2
    What characters are unwanted? All but ASCII alphanumeric? Then use `[^A-Za-z0-9]` – Wiktor Stribiżew Nov 04 '17 at 21:50
  • Oh, thanks @WiktorStribiżew, I misplaced the negation – de_dux Nov 04 '17 at 21:52
  • The problem is a long solved one, see other alternatives in the [linked thread](https://stackoverflow.com/questions/1276764/stripping-everything-but-alphanumeric-chars-from-a-string-in-python). Besides, your actual problem is that you put the `^` outside the character class and made it an anchor, rather than the negated character class qualifier (it just looks like a typo from your part). – Wiktor Stribiżew Nov 04 '17 at 21:57

0 Answers0