简体   繁体   English

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

[英]Does underscore not count as a non alphanumerical character in python?

import re

x = "dkvn_i45"

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

>>> No

I'm confused as underscore is neither a number or a letter, so surely it would count as a non-alphanumerical character? 我很困惑,因为下划线既不是数字也不是字母,因此可以肯定地将其视为非字母数字字符吗?

Docs? 文件? https://docs.python.org/3/library/re.html https://docs.python.org/3/library/re.html

\\W
Matches any character which is not a word character. 匹配不是单词字符的任何字符。 This is the opposite of \\w . 这与\\w相反。 If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_] . 如果使用ASCII标志,则等同于[^a-zA-Z0-9_] If the LOCALE flag is used, matches characters which are neither alphanumeric in the current locale nor the underscore. 如果使用LOCALE标志,则匹配在当前语言环境和下划线中都不是字母数字的字符。

(In short: no, \\W does not match on _ ) (简而言之:不, \\W_不匹配)

In regex \\W stands for any Non-Word character. 在正则表达式中\\W代表任何非Word字符。 Where Non - Word Character = anything other than letter, digit or underscore . 非单词字符 = anything other than letter, digit or underscore 字符

From above statement it should also be clear that underscore is clearly treated as a alphanumeric character. 从上面的陈述中还应该清楚地看到下划线被清楚地视为字母数字字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM