匹配非字母数字字符的正则表达式

2024-05-19 00:21:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python来解析列表中的一些字符串。某些字符串可能只包含我要忽略的非字母数字字符,例如:

list = ['()', 'desk', 'apple', ':desk', '(house', ')', '(:', ')(', '(', ':(', '))']

for item in list:
    if re.search(r'\W+', item):
        list.remove(item)

# Ideal output
list = ['desk', 'apple', ':desk', '(house']

# Actual output
list = ['desk', 'apple', '(:', '(', '))']

这是我第一次尝试使用regex来解决这个问题,但并没有达到预期的效果。如何编写regex来忽略任何带有非字母数字字符的字符串?


Tags: 字符串inapple列表foroutputif字母

热门问题