Replace non-alphanumeric characters

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
itskenny0
Posts: 1
Joined: Fri May 12, 2017 3:33 pm

Replace non-alphanumeric characters

Post by itskenny0 » Fri May 12, 2017 3:36 pm

I have a string that may contain non-alphanumeric characters and I need to ensure it only contains [^0-9a-zA-Z]. In CPython, I would do that using re.sub. Unfortunately the ure lib used in my ESP8266 build does not include this feature. What would be the best way to replicate this functionality?

Thanks in advance!

Best,
Kenny

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Replace non-alphanumeric characters

Post by deshipu » Fri May 12, 2017 7:33 pm

Code: Select all

new_string = "".join(c for c in old_string if c.isalnum())

Post Reply