Python 正则表达式

来源:互联网 发布:随意定位软件 编辑:程序博客网 时间:2024/06/03 21:41

word:

import re# .  除了换行之外任何字符   \n# \  转义字符,间接, \r  \n  \t# \d  是数字,# \D  非数字# \s  空白字符,空格,\t# \S  非空白字符# \w  单词字符,大小写字母数字# \W  非单词字符regex=re.compile(r"\W",re.IGNORECASE)print(regex.match("+"))
中括号[]:

import re#[abcd] abcd中任取一个#[^abcd],第一个字符不是abcd#[0-9]数字#[^0-9]非数字#[a-z]小字母#[^a-z]非小写字母#[a-zA-Z0-9] 字母和数字中的任一个#[]代表一个字符  [][]代表两个字符regex=re.compile("[0-9][a-z]",re.IGNORECASE) #re.IGNORECASE忽略异常,忽略大小写print(regex.match("9F"))
次数:

import re# *表示0或者多次# +表示1或者多次# ?表示1或者0次# {2} 代表2次# {1,6} 代表1-6次regex=re.compile("\\d{1,3}",re.IGNORECASE)print(regex.match("9"))print(regex.match("91"))print(regex.match("91w"))print(regex.match("91asds13213"))print(regex.match("asds13213"))print(regex.match("13213"))





原创粉丝点击