[英]Python regex to determine whether a character is numeric, alphanumeric or in a specified string
I am unsure as to how one would use a Python regex to determine whether a character is numeric, alphanumeric or in a specified string. 我不确定如何使用Python正则表达式来确定字符是数字,字母数字还是指定的字符串。
Something like (fake code warning): 类似于(伪造的代码警告):
if 'a' in re.['A-Z']:
print "Alpha"
if '.' in re.['.,;']:
print "Punctiation"
Use str.isalpha()
method: 使用
str.isalpha()
方法:
>>> 'a'.isalpha()
True
For testing single character for punctuation or alphanumeric, you can use constants pre-defined in string
module: 为了测试标点符号或字母数字的单个字符,可以使用
string
模块中预定义的常量:
>>> '.' in string.punctuation:
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.