简体   繁体   English

替换字符串中的所有非字母数字字符

[英]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.

相关问题 从字符串中获取列表,删除所有非字母数字字符 - Obtain a list from a string removing all non-alphanumeric characters 在 Python 中使用 RegEx 替换除一种特定模式之外的所有非字母数字字符 - Replace all Non-Alphanumeric Characters except one particular pattern using RegEx in Python 大多数 Pythonic 是从字符串中去除所有非字母数字前导字符 - Most Pythonic was to strip all non-alphanumeric leading characters from string 如何使用正则表达式从字符串中删除所有非字母数字字符(“#”除外)? - How can I remove all non-alphanumeric characters from a string, except for '#', with regex? Python:如何拆分字符串但保留非字母数字字符 - Python: How to split string but preserve the non-alphanumeric characters 替换python中括号之间的所有非字母数字异常 - Replace all non-alphanumeric exception between brackets in python 如何删除字符串开头或结尾的非字母数字字符 - How to rermove non-alphanumeric characters at the beginning or end of a string 从 Python 中的字符串中删除非字母数字 unicode 字符 - Removing non-alphanumeric unicode characters from a string in Python 将字符串转换为Python中所有非字母数字字符的列表 - Turning a string into a list of all its non-alphanumeric chars in Python 使用非字母数字字符过滤掉行 - Filtering out rows with non-alphanumeric characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM