regex to remove all special characters but / in python

You can use regex to remove all special characters except for the forward slash "/" character by using a character class.

Here's an example code snippet that shows how to do it:

main.py
import re

input_string = "This is a string with!@#$%^&*()_+ some special characters / that we want to keep."

# Define the pattern to match any character that is not a word character or forward slash
pattern = r"[^\w/]"

# Use re.sub to replace any matched characters with an empty string
processed_string = re.sub(pattern, "", input_string)

print(processed_string)
368 chars
12 lines

In this example, we define a pattern that matches any character that is not a word character (i.e. a letter or digit) or a forward slash. We then use the re.sub() function to replace any matched characters with an empty string, effectively removing them from the string.

After running this code, the processed_string variable will contain the original string with all special characters removed except for the forward slash.

gistlibby LogSnag