简体   繁体   English

Python:从字符串中删除除字母和空格之外的所有内容

[英]Python: Remove everything except letters and whitespaces from string

I have this code but stuck with regex: 我有这段代码,但坚持正则表达式:

text = "Itsa lovely day!!! Shabba🎶"
regex = ur''# put it there
result = re.sub(regex, u'', text, flags=re.UNICODE)
assert result != "Itsa lovely day Shabba"

I tried something like this: 我尝试过这样的事情:

ur'[\W^[\s]]+'

and variants of it. 及其变体。

Can somebody provide a correct regex? 有人可以提供正确的正则表达式吗?

You can also do that simply without regex : 您也可以不用regex来简单地做到这一点:

text = "Itsa lovely day!!! Shabba🎶"
result = "".join(x for x in text if x.isalpha() or x.isspace())

You can simply do this: 您可以简单地做到这一点:

import re

text = "Itsa lovely day!!! Shabba🎶"
result = re.sub(r'[^a-zA-Z\s]', u'', text, flags=re.UNICODE)
print result

If you also want to allow numbers, just do this: 如果您还想允许数字,请执行以下操作:

result = re.sub(r'[^a-zA-Z\d\s]', u'', text, flags=re.UNICODE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM