I want my string to only have alphanumeric characters, -, and underscores. Thats it. I am trying to write a method that takes in a user input string and converts it so that it follows the guideline.
My regex is obviously a-zA-Z0-9_-
. What I want to do is replace all the spaces with the -, and just remove all the other characters that don't fall under my regex.
So, the string 'Hello, world!'
would get converted into 'Hello-world'
. The special characters get removed, and the space is replaced with a -.
What would be the most efficient way to do this using python? Do I have to iterate over the entire string character by character, or is there a better way? Thanks!