通过 python 正则表达式删除包含特殊字符的字符串
remove string which contains special character by python regular expression
我的代码:
s = '$ascv abs is good'
re.sub(p.search(s).group(),'',s)
输出:
'$ascv abs is good'
我想要的输出:
'abs is good'
我想通过 python 正则表达式删除包含特殊字符的字符串。我以为我的代码是正确的,但输出是错误的。
如何修改我的代码以使输出正确?
invalid_chars = ['@'] # Characters you don't want in your text
# Determine if a string has any character you don't want
def if_clean(word):
for letter in word:
if letter in invalid_chars:
return False
return True
def clean_text(text):
text = text.split(' ') # Convert text to a list of words
text_clean = ''
for word in text:
if if_clean(word):
text_clean = text_clean+' '+word
return text_clean[1:]
# This will print 'abs is good'
print(clean_text('$ascv abs is good'))
我的代码:
s = '$ascv abs is good'
re.sub(p.search(s).group(),'',s)
输出:
'$ascv abs is good'
我想要的输出:
'abs is good'
我想通过 python 正则表达式删除包含特殊字符的字符串。我以为我的代码是正确的,但输出是错误的。
如何修改我的代码以使输出正确?
invalid_chars = ['@'] # Characters you don't want in your text
# Determine if a string has any character you don't want
def if_clean(word):
for letter in word:
if letter in invalid_chars:
return False
return True
def clean_text(text):
text = text.split(' ') # Convert text to a list of words
text_clean = ''
for word in text:
if if_clean(word):
text_clean = text_clean+' '+word
return text_clean[1:]
# This will print 'abs is good'
print(clean_text('$ascv abs is good'))