Joshua Joshua - 2 years ago 156
Python Question

python substring with unknown symbol

I have a string that I need to edit in python. Lets say the string is: thisandthat+this. The string could be any length.

Now pretend you don't know what/where in the string the special characters are. For example these characters: +-)(&^%$#@!*

How do I grab everything except for the special characters? So for the above example my result would be thisandthatthis.

Answer Source

If space is not included in special, you can use

''.join(re.split("[^a-z]", input_str))

You can also use re.sub

re.sub("[^a-z]", "", input_str)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download