zoukankan      html  css  js  c++  java
  • python 正则表达式练习题

    习题集一

    习题1:判断是否匹配成功,并输出对应匹配信息

    import re

    source = "1huhongqiang"

    if re.match("hu",source):#if re.match is not None

        print("可以匹配到")

    else:

        print ("没有匹配到")

    if re.search("hu",source):

         print ("可以匹配到")

    else:

    print ("没有匹配到")

    习题2: 找出一个字符串中是否有连续的5个数字

    >>> print re.search(r"d{5}","1234aadd222222").group()

    22222

    习题3:出一个字符串中的连续5个数字,要求数字前后必须是非数字

     

    >>> re.search(r'(Dd{5}D)|(^d{5}D)|(Dd{5}$)|(^d{5}$)','12567').group()

    '12567'

    习题4:统计一个文件中单词的数量

    with open("d:\word.txt","r") as file_obj:

        print(len(re.findall(r"([A-Za-z]+)",file_obj.read())))

    习题5:a1b23c4d非字符内容拼成一个字符串

    >>> "".join(re.findall(r"[^A-Za-z]","a1b23c4d"))

    '1234'

    习题6:取最后一个字母

    >>> re.findall(r"[A-Za-z]","ab12cd")[-1]

    'd'

    >>> re.search(r"[A-Za-z]$","ab12cd").group()

    'd'

    习题7:找出一个字符串中的所有数字

    >>> pattern = re.compile(r"d+")

    >>> pattern.findall("a1cd33dd99kddd")

    ['1', '33', '99']

    >>> pattern = re.compile(r"d")

    >>> pattern.findall("a1cd33dd99kddd")

    ['1', '3', '3', '9', '9']

     

    习题8:把一个字符串中的所有字母找出并拼成一个字符串

    >>> pattern = re.compile(r"[A-Za-z]")

    >>> "".join(pattern.findall("a1cd34dsf0dsfkjk"))

    'acddsfdsfkjk'

    习题9:输出句子中的所有单词

    >>> s = "I am a boy! you are a girl!"

    >>> pattern = re.compile(r"([A-Za-z]+)")

    >>> pattern.findall(s)

    ['I', 'am', 'a', 'boy', 'you', 'are', 'a', 'girl']

    习题集二:

    1、匹配一行文字中的所有开头的字母内容

    import re

    s="i love you not because of who you are!"

    print(re.findall(r"w",s))

    2、匹配一行文字中的所有开头的数字内容

    import re

    s="12i love 34you not 56because of 7who 8999!"

    print(re.findall(r"d",s))

    3、匹配一行文字中的所有开头的数字内容或数字内容

    import re

    s="12i love 34you not 56beca11use of 7who 8999!"

    print(re.findall(r"d+",s))

    4、 只匹配包含字母和数字的行

    import re

    s="because 12sd 34er 56 df e4 54434"

    print(re.findall(r"w+",s,re.M))

    5、写一个正则表达式,使其能同时识别下面所有的字符串:'bat','bit', 'but', 'hat', 'hit', 'hut

    import re

    s="'bat', 'bit', 'but', 'hat', 'hit', 'hut','yat','har','hot'"

    print(re.findall(r"..t",s))

    #print(re.findall(r"[bh][aiu]t",s))

    6、匹配所有合法的python标识符

    import re

    s="awoeur awier !@# @#4_-asdf3$^&()+?><dfg$ $"

    print(re.findall(r".*",s,re.DOTALL))

    7、提取每行中完整的年月日和时间字段

    import re
    s="我出生时间为1990-01-01 00:00:00,今天时间为2019-04-20 12:20:00"

    for i in s.split(","):
         result=re.search(r"[12][0-9]{3}-([0][1-9]|[1][0-2])-([0-2][0-9]|[3][01]) ([01][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])",i)
         if result:
             print(result.group())
         else:
             continue

    结果为:

    1990-01-01 00:00:00

    2019-04-20 12:20:00

    8、将每行中的电子邮件地址替换为你自己的电子邮件地址

    # coding:utf-8
    import re
    s="""
    1234867@qq.com
    lihuali@sdcion.com
    """
    s1="guolingping@sdcion.com"
    for i in s.split(" "):
        result=re.search(r"[w]+@[w]+.com",i)
        if result:
            s=s.replace(result.group(),s1)
        else:
            continue
    print(s)

    9、匹配home关键字:

    import re
    s="123ahjfhhome123homertuy"
    for i in re.findall(r"\home",s):
        print(i)

    10、使用正则提取出字符串中的单词

    # coding:utf-8
    import re
    s="I am a boy, my is 19 year!"
    print(" ".join(re.findall(r"[a-zA-Z]+",s)))

    11、使用正则表达式匹配合法的邮件地址:

    国际域名格式如下:

    域名由各国文字的特定字符集、英文字母、数字及-(即连字符或减号)任意组合而成, 但开头及结尾均不能含有“-”,“-”不能连续出现。域名中字母不分大小写。域名最长可达60个字节(包括后缀.com.net.org)

    import re
    s="lisi_1234@qq.org"
    result=re.match(r"^[w]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?$",s)
    if result:
        print(result.group())

    12、提取字符串中合法的超链接地址比如:s = '<a href="http://www.gloryroad.cn">光荣之路官网</a>'要求,给出的正则表达式能兼顾所有链接地址。

    import re
    s='<a href="http://www.gloryroad.cn">光荣之路官网</a>'
    print(re.search(r'w+://[w]{3}.w+.w{2,3}',s).group())

    13、统计文件中单词个数

    # coding:utf-8
    import re
    s="I am a boy, my is 19 year!"
    result=re.findall(r"[a-zA-Z]+",s)
    print(result)
    print("单词个数为:",len(result))

    14、写一个函数,其中用正则验证密码的强度

    import re
    """
    密码长度大于或等于8位数
    强:字母+数字+特殊字符
    中:字母+数字,字母+特殊字符,数字+特殊字符
    弱:纯数字,纯字母,纯特殊字符
    """

    def checklen(pwd):
        if len(pwd)>=8:
            return True
        else:
            return False

    def is_strong_pwd(pwd):
        pattern=re.compile(r"^(?![a-zA-z]+$)(?!d+$)(?![!@#$%^&*]+$)(?![a-zA-zd]+$)(?![a-zA-z!@#$%^&*]+$)(?![d!@#$%^&*]+$)[a-zA-Zd!@#$%^&*]+$")
        result=pattern.findall(pwd)
        if result:
            return True
        else:
            return False

    def is_inter_pwd(pwd):
        pattern=re.compile(r"^(?![a-zA-z]+$)(?!d+$)(?![!@#$%^&*]+$)[a-zA-Zd!@#$%^&*]+$")
        result=pattern.findall(pwd)
        if result:
            return True
        else:
            return False

    def is_weak_pwd(pwd):
        pattern=re.compile(r"^(?:d+|[a-zA-Z]+|[!@#$%^&*]+)$")
        result=pattern.findall(pwd)
        if result:
            return True
        else:
            return False

    def checkpassword(pwd):

        #判断密码长度是否合法
        lenOK=checklen(pwd)

        #判断是否强:字母+数字+特殊字符
        strongOK=is_strong_pwd(pwd)

        #判断是否中:字母+数字,字母+特殊字符,数字+特殊字符
        interOK=is_inter_pwd(pwd)

        #判断是否弱:纯数字,纯字母,纯特殊字符
        weakOK=is_weak_pwd(pwd)

        print(lenOK)
        print(strongOK)
        print(interOK)
        print(weakOK)
        if lenOK:
            if strongOK:
                print("密码的强度为强的!")
            elif interOK:
                print("密码的强度为中的!")
            elif weakOK:
                print("密码的强度为弱的,建议修改!")
        else:
            print("密码长度不合格!")

    checkpassword("Helloworld#123")

    结果为:

    True

    True

    True

    False

    密码的强度为强的!

    15、匹配ip的正则表达式:

    r'^(([1-9]|[1-9]d|1dd|2[0-4]d|25[0-5]).){3}([1-9]|[1-9]d|1dd|2[0-4]d|25[0-5])$'

    # coding:utf-8
    import re
    s='172.16.23.189'
    s1='11.2.123.1'
    s2='255.255.255.255'
    s2='0.0.0.0'
    pattern=re.compile(r'^(([1-9]|[1-9]d|1dd|2[0-4]d|25[0-5]).){3}([1-9]|[1-9]d|1dd|2[0-4]d|25[0-5])$')
    print(pattern.match(s).group())

  • 相关阅读:
    zabbix验证微信
    free
    有名管道和无名管道
    shell实现并发控制
    TCP/IP协议簇 端口 三次握手 四次挥手 11种状态集
    自动化运维
    JSON对象(自定义对象)
    对象中属性的遍历、删除与成员方法
    对象间的赋值操作
    自定义类
  • 原文地址:https://www.cnblogs.com/guo-123/p/10745289.html
Copyright © 2011-2022 走看看