下划线在python中不算作非字母数字字符吗?

2024-09-30 16:38:46 发布

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

import re

x = "dkvn_i45"

if re.search(r"\W", x):
  print("Yes")
else: 
  print("No")

>>> No

我很困惑,因为下划线既不是数字也不是字母,所以它肯定会算作非字母数字字符吗?你知道吗


Tags: noimportresearchif字母数字字符
2条回答

医生?https://docs.python.org/3/library/re.html

\W
Matches any character which is not a word character. This is the opposite of \w. If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_]. If the LOCALE flag is used, matches characters which are neither alphanumeric in the current locale nor the underscore.

(简而言之:否,\W_不匹配)

在regex中\W代表任何非单词字符。 其中非单词字符=anything other than letter, digit or underscore。你知道吗

从上面的语句中还应该清楚地看到,下划线被清楚地视为字母数字字符。你知道吗

相关问题 更多 >