• 正则的简单学习与应用


    import re
    #- match:从开头进行匹配,匹配到就返回正则结果对象,没有返回None。
    m=re.match('abc','abcdaskjabcsdaj')
    print(m.group())#返回要匹配的内容
    print(m.span())# 返回匹配内容位置
    # 匹配所有内容,找到返回所有匹配的内容列表,没有找到返回空列表
    f=re.findall('abcd','adsjkjdabcajsdlasabcjsdlaabc')
    print(f)
    # 先生成正则表达式对象
    c = re.compile('hello')
    
    print(type(c))
    # 从开头匹配
    m = c.match('hellosadk;ask;kahellosadhlkas')
    print(m)
    
    # 从任意位置匹配
    s = c.search('hadlsjasdhellokjsdlks')
    print(s)
    
    # 匹配所有内容
    f = c.findall('hellosdhasdjahelloshdajldhello')
    print(f)
    
    patterns=re.compile('^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,30}$')
    
    
    
    
    while True:
        pasword = input('请输入密码:')
        f = patterns.findall(pasword)
        if not f:
            print('密码不规范,请重新输入:(密码必须包含数字、字母、特殊符号,且长度不能小于8)')
        else:
            print(f)
            print('密码设置成功')

    上面是一些简单的练习,比较适合初学者,适合入门,写得不好勿喷

    下面是打印出的结果

    abc
    (0, 3)
    []
    <class '_sre.SRE_Pattern'>
    <_sre.SRE_Match object; span=(0, 5), match='hello'>
    <_sre.SRE_Match object; span=(9, 14), match='hello'>
    ['hello', 'hello', 'hello']
    请输入密码:
  • 相关阅读:
    多层开发的小知识
    DIV+CSS基础教程:导航条的制作详解
    JavaScript函数
    css:学习CSS了解单位em和px的区别
    blank开新窗口为什么通不过W3C验证
    对javascript匿名函数的理解(透彻版)
    .net如何与windows身份验证的sql数据库连接
    Aptana2.0系列教程
    C# Tostring() 格式大全
    类关系图
  • 原文地址:https://www.cnblogs.com/liangliangzz/p/11271845.html
Copyright © 2020-2023  润新知