我很新的编程,我试图用正则表达式来打印包含只有字母的字符串。但我有一个问题。我也想保留任何空格。所以这是我:如何保留空白(正则表达式)
value = 'h&(()^%el!!l000o [email protected]!ld'
import re
value = ''.join(re.findall('[a-zA-Z]+',value))
print value
helloworld
,我想输出是:世界你好。我认为问题的一部分是我使用.join。我怎样才能保留空白,但也确保只有字母打印?
我很新的编程,我试图用正则表达式来打印包含只有字母的字符串。但我有一个问题。我也想保留任何空格。所以这是我:如何保留空白(正则表达式)
value = 'h&(()^%el!!l000o [email protected]!ld'
import re
value = ''.join(re.findall('[a-zA-Z]+',value))
print value
helloworld
,我想输出是:世界你好。我认为问题的一部分是我使用.join。我怎样才能保留空白,但也确保只有字母打印?
您需要匹配正则表达式中的空白以及匹配字母。您可以通过添加\s
你的正则表达式如下做到这一点:
import re
value = 'h&(()^%el!!l000o [email protected]!ld'
value = ''.join(re.findall('[a-zA-Z\s]+',value))
print value
输出
hello world
使用'””。加入(...)'代替。 – zondo
那不行? – gtlambert