regular 1


'''
  re 模块
  正则表达式: regular Expression
  QQ: 5位-11位 纯数字  不能0开头
  邮箱验证: [email protected]
  用户名:

  match(): 从字符串的开始进行匹配,如果开始都不能匹配成功,则不会继续向下搜索
  其中:
     . 表示任意字符
    [] 表示的是一个范围, 可以是[a-z],或者[abc]  但是只是表示一个字符,
    如果表示多个字符则需要使用量词。
    量词使用:
        * :  >=0
        + :  >=1
        ? :  0 或者 1
        {n}: 具体的个数n
        {n,}: >=n
        {n,m}: m到n之间的个数
    ^ $
'''
import re
from re import match

name = 'admin'

# 不能匹配则返回None,
result = match('abc', 'helloabc')
print(result)

result = match('abc', 'abchello')
print(result)

span = result.span()  # 就是匹配位置
print(span)

group = result.group()  # 匹配内容
print(group)

print('----------------------------')

# a任意一个字符c,正则中’.‘ 任意字符串(除换行符)
result = match('a.c', 'a#chello')
print(result)

# ac之间只能是字母  [xyz] 三个字母中的任意一个, [x-z]从x到z中的任意一个
result = match('a[a-z]c', 'aSc123', re.I)
print(result)  # None

# 第一个必须是字母,后面2位可以是字母和数字
result = match('[a-z][a-z0-9][a-z0-9]', 'a11123', re.I)
print(result)

# 第一个必须是字母,后面4位可以是字母,数字,下划线
# \w  ~ [0-9a-zA-Z_]
# 正则的量词
# + * ? {}
# {4}
result = match('[a-z]\w{4}', 'a1_h')
print(result)

# 第一个必须是字母,后面必须是字母,数字,下划线,总长最长8位
# {1,7}  7>len>1

result = match('[a-z]\w{4,7}', 'a1235')
print(result)

# 第一个必须是字母,后面必须是字母,数字,下划线,总长度至少6位
# {5,}  len>=5
result = match('[a-z]\w{5,}', 'a1235')
print(result)

# 第一个必须是字母,后面必须是字母,数字,下划线,总长度至少2位
# +      >=1
result = match('[a-z]\w+', 'a1235')
print(result)

# *  >=0
result = match('[a-z]\w*', 'a')
print(result)

# ?  0或者1
result = match('[a-z]\w?', 'a1677')
print(result)

#  ^开始  $ 结束
result = match('^[a-z]\w?$', 'a1677')
print(result)

# jdksfjlk

# 输入用户名和密码,
# 用户名:只能是字母数字,下划线,而且不能数字开头,用户名的长度6位以上12位以下
# 密码: 必须是6位以上的纯数字

 

Publicado 256 artigos originais · ganhou elogios 6 · vista 3502

Acho que você gosta

Origin blog.csdn.net/piduocheng0577/article/details/105107011
Recomendado
Clasificación