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

2024-04-26 18:36:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有特殊字符的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')

Tags: 函数字符串编辑编码string单词建议technologies
2条回答

您提供的信息不多,因此这可能与您想要的信息接近,也可能不接近:

import re
origstr = "msdjdgf(^&%(Aroha Technologies&^$^&^CHJdjg"
match = re.search("[A-Z][a-z]*(?: [A-Z][a-z]*)*", origstr)
if match:
    newstr = match.group()

(查找一系列大小写之间有空格的单词)

在这里,更脏一点的方法

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

希望这有帮助;)

相关问题 更多 >