我正在研究一个保留一个单词(一个单词可以包含a-zA-Z,0-9和_)的符号的函数,但是除去每个其他符号去掉一个字符串中符号的替代方法
For example:
Input String - hell_o ? my name _ i's <hel'lo/>
Output - ['hell_o' ,'my', 'name', '_', "i's" ,'hel'lo']
我使用的功能:单词外
l = ' '.join(filter(None,(word.strip(punctuation.replace("_","")) for word in input_String.split())))
l = re.sub(r'\s+'," ",l)
t = str.split(l.lower())
我知道这是不是最好的,最佳的方式!没有人建议我可以尝试?也许正则表达式的任何替代方案去做这个??
我试着使用: 负环顾四周,看看屁股:
\W+(?!\S*[a-z])|(?<!\S)\W+
s.strip(punctuation)
re.sub('[^\w]', ' ', doc.strip(' ').lower())
- 这消除了字里面标点符号太
如果你* *知道这是不是'''最好的,optimal'''的方式,那么你心里一定有一些替代品 - 请列出那些(在这个问题)以及你拒绝他们的原因,所以我们知道什么不能工作。 – wwii