
| Current Path : /proc/thread-self/root/usr/local/lib/python3.8/dist-packages/txt/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : //proc/thread-self/root/usr/local/lib/python3.8/dist-packages/txt/remove_non_alphanumeric.py |
import re
def remove_non_alphanumeric(s, replace_with='_', keep_underscore=True):
s = str(s)
# replace & with and
s = re.sub(r'&+', '&', s)
s = s.replace(' & ', ' and ')
s = s.replace('_&_', '_and_')
s = s.replace('&', ' and ')
if keep_underscore:
return re.sub(r'[\W]+', replace_with, s, flags=re.UNICODE)
else:
return re.sub(r'[\W_]+', replace_with, s, flags=re.UNICODE)
def remove_non_alphabetic(s):
return ''.join([i for i in s if i.isalpha()])