美文网首页
正则表达式练习

正则表达式练习

作者: tonyemail_st | 来源:发表于2017-11-09 09:11 被阅读0次

    匹配邮箱

    Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> y='123@qq.comaaa@163.combbb@126.comasdfasfs33333@adfcom'
    >>> import re
    >>> re.findall('\w+@(qq|163|126).com', y)
    ['qq', '163', '126']
    >>> re.findall('\w+@(?:qq|163|126).com', y)
    ['123@qq.com', 'aaa@163.com', 'bbb@126.com']
    

    匹配电话号码

    >>> y = 'tel:010-12345678 address:changanRoad'
    >>> re.findall('\d', y)
    ['0', '1', '0', '1', '2', '3', '4', '5', '6', '7', '8']
    >>> re.findall('\d+', y)
    ['010', '12345678']
    >>> re.findall('\d+-', y)
    ['010-']
    >>> re.findall('\d+-\d+', y)
    ['010-12345678']
    

    匹配本地磁盘路径,取出“C:\code\cnkiCrawl”

    >>>y = 'localpath C:\code\cnkiCrawl'
    >>>re.findall('[a-zA-Z]:\\\\\w+\\\\\w+', y)
    ['C:\\code\\cnkiCrawl']
    

    匹配Hello Kitty Hello Hello Kitty Kitty Hello Kitty中的“Hello Hello Kitty Kitty”

    >>> re.findall('(?:Hello ){2}(?:Kitty ){2}', y)
    ['Hello Hello Kitty Kitty ']
    

    匹配aabaaabaaaab中的aab,而不匹配aaab,aaaab

    相关文章

      网友评论

          本文标题:正则表达式练习

          本文链接:https://www.haomeiwen.com/subject/fbxpmxtx.html