zoukankan      html  css  js  c++  java
  • 正则的简单学习与应用

    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']
    请输入密码:
  • 相关阅读:
    api接口统一管理
    axios封装
    事件监听和事件模型
    W3C标准
    Redis安装(PHPredis服务+windows的redis环境)
    Redis介绍
    jQuery ajax方法小结
    博客园鼠标特效
    PHP---截取七牛地址中的文件名
    jQuery---显示和隐藏
  • 原文地址:https://www.cnblogs.com/liangliangzz/p/11271845.html
Copyright © 2011-2022 走看看