I have a string with which i want to replace any character that isn't a standard character or number such as [a-z or 0-9] with an asterisk. For example, "h^&ell`.,|o w]{+orld" is replaced with "h*ell*o*w*orld". Note that multiple characters such as "^&" get replaced with one asterisk. How would I go about doing this?
nneonneo
165k35 gold badges293 silver badges368 bronze badges
asked Oct 20, 2012 at 5:10
2
Regex to the rescue!
import re
s = re.sub['[^0-9a-zA-Z]+', '*', s]
Example:
>>> re.sub['[^0-9a-zA-Z]+', '*', 'h^&ell`.,|o w]{+orld']
'h*ell*o*w*orld'
answered Oct 20, 2012 at 5:11
nneonneonneonneo
165k35 gold badges293 silver badges368 bronze badges
6
The pythonic way.
print "".join[[ c if c.isalnum[] else "*" for c in s ]]
This doesn't deal with grouping multiple consecutive non-matching characters though, i.e.
"h^&i => "h**i
not
"h*i"
as in the regex solutions.
crizCraig
7,9185 gold badges53 silver badges52 bronze badges
answered Feb 28, 2014 at 13:27
baloanbaloan
6555 silver badges7 bronze badges
Try:
s = filter[str.isalnum, s]
in Python3:
s = ''.join[filter[str.isalnum, s]]
Edit: realized that the OP wants to replace non-chars with '*'. My answer does not fit
answered Jan 5, 2015 at 5:15
DonDon
16.4k11 gold badges61 silver badges97 bronze badges
0
Use \W
which is equivalent to [^a-zA-Z0-9_]
. Check the documentation, //docs.python.org/2/library/re.html
import re
s = 'h^&ell`.,|o w]{+orld'
replaced_string = re.sub[r'\W+', '*', s]
output: 'h*ell*o*w*orld'
update: This solution will exclude underscore as well. If you want only alphabets and numbers to be excluded, then solution by nneonneo is more appropriate.
Csaba Toth
9,2265 gold badges70 silver badges112 bronze badges
answered Aug 12, 2016 at 18:54
psunpsun
5659 silver badges13 bronze badges
2