Skip to main content
 首页 » 编程设计

python之使用python删除字符串中的特殊字符

2025年05月04日31exmyth

我有一个带有特殊字符的 string = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg"。

我正在尝试的是删除字符串中的所有特殊字符,然后显示“Aroha Technologies”一词

我能够使用 lstrip() 函数进行硬编码,但任何人都可以帮我解决如何使用正则表达式在一行中显示字符串“Aroha Technologies”。

编辑建议:-

通过使用此 lstrip() 和 rstrip() 函数,我能够从字符串中删除字符。

str = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg" 
 
str=str.lstrip('msdjdgf(^&%*(') 
 
str=str.rstrip('&^$^&*^CHJdjg') 

请您参考如下方法:

这里,有点肮脏的方法

import re # A module in python for String matching/operations   
a = "msdjdgf(^&%*(Aroha Technologies&^$^&*^CHJdjg" 
stuff = re.findall('\W(\w+\s\w+)\W', a) 
print(stuff[0]) # Aroha Technologies 

希望这有帮助;)