Skip to main content
 首页 » 编程设计

python之使用 RegEx 从 Python 字符串的开头去除所有非字母字符

2025年05月04日23kerrycode

我有各种单词列表,其中很多都是嘈杂的。我所说的嘈杂是指它以一些非字母字符开头,例如 ' "'、' - '。例如:"thisword, -thisword , -"this word, .thisword 并且可以是其他几个。

就像我们可以使用

删除 ascii
from string import ascii letter 
string.lstrip(ascii_letters)   

在python中有没有类似的方法可以不使用正则表达式来处理non_ascii?

谢谢!

请您参考如下方法:

你为什么不使用 string.puctuation

>>> from string import punctuation 
>>> "-asdf".lstrip(punctuation) 
'asdf' 
>>> "'asdf".lstrip(punctuation) 
'asdf' 
>>> '"asdf'.lstrip(punctuation) 
'asdf' 
>>> ',asdf'.lstrip(punctuation) 
'asdf'