简体   繁体   English

Python正则表达式来确定字符是数字,字母数字还是指定的字符串

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

You can use the match function from module re : 您可以使用re模块中的match函数:

import re

x = 'a'
if re.match('[a-zA-Z]', x):
    print "Alpha"

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

相关问题 如何仅为 Python 中的特定数字字符重新格式化字母数字字符串? - How to reformat an alphanumeric string only for a specific numeric character in Python? 判断Python对象是正则表达式还是字符串 - Determine whether Python object is regex or string 如何从字符串 python 正则表达式的中间消除对字母数字字符的检查 - how to eliminate checking of alphanumeric character from middle of string python regex 使用python的数字到字母数字编码器(置换和正则表达式) - Numeric to alphanumeric encoder using python (permutation and regex) 检测 python 字符串中的字母数字/数字值 - Detecting alphanumeric/numeric values in python string 获取第一个数字字符 python 正则表达式之前的所有字符串 - get all the string before first numeric character python regex Python,正则表达式用多个分隔符分割字母数字字符串 - Python, regex to split alphanumeric string with multiple separators 正则表达式从 python 中的文本中识别固定字符字母数字词 - Regex to Identify Fixed character alphanumeric word from text in python 正则表达式 Python 与 min 一个字母、一个数字和 min 一个非字母数字字符 - Regex Python with min a letter, a number and min a non-alphanumeric character python:递归检查以确定字符串是否是回文 - python: recursive check to determine whether string is a palindrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM