Am working with Python, trying to allow only alphanumeric characters, commas, dots, spaces
When I run the code below, it throws error invalid syntax:
strx ='test, 22222 @% te-st test test'
result = strx.replace(/[^A-Za-z0-9, .]/g, '')
print(result)
1
In Python, regular expressions are handled by the re module.
The correct way to create a regex for allowing only alphanumeric characters, commas, dots, and spaces is to use the re.sub function to substitute unwanted characters with an empty string.
import re
strx = 'test, 22222 @% te-st test test'
result = re.sub(r'[^A-Za-z0-9, .]', '', strx)
print(result)