Reputation: 339
I'm pretty new programming and I'm trying to use regex to print strings containing only letters. But I'm having one problem. I also want to preserve any whitespace. So this is what I have:
value = 'h&(()^%el!!l000o w@0or!ld'
import re
value = ''.join(re.findall('[a-zA-Z]+',value))
print value
helloworld
The output that I want is: hello world. I think part of the problem is that I'm using .join. How can I preserve the whitespace but also make sure that only letters are printing?
Upvotes: 1
Views: 2619
Reputation: 11961
You need to match whitespace in your regex, as well as matching letters. You can do this by adding \s
to your regex as follows:
import re
value = 'h&(()^%el!!l000o w@0or!ld'
value = ''.join(re.findall('[a-zA-Z\s]+',value))
print value
Output
hello world
Upvotes: 2