refactor: syntax cleanup (#172)

This commit is contained in:
gasolin
2023-07-07 18:40:10 +08:00
committed by GitHub
parent 5e6aef1e37
commit f08ee6420a
2 changed files with 6 additions and 7 deletions

View File

@@ -13,21 +13,20 @@ def clean_string(text):
"""
# Replacement of newline characters:
text = text.replace('\n', ' ')
# Stripping and reducing multiple spaces to single:
cleaned_text = re.sub(r'\s+', ' ', text.strip())
# Removing backslashes:
cleaned_text = cleaned_text.replace('\\', '')
# Replacing hash characters:
cleaned_text = cleaned_text.replace('#', ' ')
# Eliminating consecutive non-alphanumeric characters:
# This regex identifies consecutive non-alphanumeric characters (i.e., not a word character [a-zA-Z0-9_] and not a whitespace) in the string
# and replaces each group of such characters with a single occurrence of that character.
# For example, "!!! hello !!!" would become "! hello !".
cleaned_text = re.sub(r'([^\w\s])\1*', r'\1', cleaned_text)
return cleaned_text
return cleaned_text