[英]Replace all non-alphanumeric characters in a string
I have a string with which i want to replace any character that isn't a standard character or number such as (az or 0-9) with an asterisk. 我有一个字符串,我想用一个星号替换任何非标准字符或数字的字符,如(az或0-9)。 For example, "h^&ell`.,|ow]{+orld" is replaced with "h*ell*o*w*orld".
例如,“h ^&ell`。,| ow] {+ orld”被替换为“h * ell * o * w * orld”。 Note that multiple characters such as "^&" get replaced with one asterisk.
请注意,多个字符(如“^&”)将替换为一个星号。 How would I go about doing this?
我该怎么做呢?
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'
The pythonic way. pythonic的方式。
print "".join([ c if c.isalnum() else "*" for c in s ])
This doesn't deal with grouping multiple consecutive non-matching characters though, ie 这并不涉及对多个连续的非匹配字符进行分组,即
"h^&i => "h**i
not "h*i"
as in the regex solutions. "h^&i => "h**i
不像正则表达式解决方案中那样"h*i"
。
Use \\W
which is equivalent to [^a-zA-Z0-9_]
. 使用
\\W
等于[^a-zA-Z0-9_]
。 Check the documentation, https://docs.python.org/2/library/re.html 查看文档, https://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.
如果您只想排除字母和数字,那么nneonneo的解决方案更合适。
Try: 尝试:
s = filter(str.isalnum, s)
in Python3: 在Python3中:
s = ''.join(filter(str.isalnum, s))
Edit: realized that the OP wants to replace non-chars with '*'. 编辑:意识到OP想要用'*'替换非chars。 My answer does not fit
我的回答不合适
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.