Replacing all non-alphanumeric characters with empty strings
Use [^A-Za-z0-9]. Note: removed the space since that is not typically considered alphanumeric.
Use [^A-Za-z0-9]. Note: removed the space since that is not typically considered alphanumeric.
I just timed some functions out of curiosity. In these tests I’m removing non-alphanumeric characters from the string string.printable (part of the built-in string module). The use of compiled ‘[\W_]+’ and pattern.sub(”, str) was found to be fastest. $ python -m timeit -s \ “import string” \ “”.join(ch for ch in string.printable if ch.isalnum())” 10000 … Read more