Python - Re正则表达式

这里整理下Python中Re正则表达式的用法(☆▽☆)

    import re       #导入正则表达式
    
    #简单 Python 匹配
    pattern1 = "cat"
    pattern2 = "bird"
    string = "dog runs to cat"
    print(pattern1 in string)    
    print(pattern2 in string)
    
    #用正则寻找配对
    pattern1 = "cat"
    pattern2 = "bird"
    string = "dog runs to cat"
    print(re.search(pattern1, string))           #用search()函数在string变量里匹配pattern1里的字符串
    print(re.search(pattern2, string))           #用search()函数在string变量里汽配pattern2里的字符串
    
    #匹配多种可能 使用 []
    # multiple patterns ("run" or "ran")
    ptn = r"r[au]n"       
    print(re.search(ptn, "dog runs to cat"))
    
    #匹配更多种可能
    #continue
    print(re.search(r"r[A-Z]n", "dog runs to cat"))        #匹配A-Z范围里的字符
    print(re.search(r"r[a-z]n", "dog runs to cat"))        #匹配a-z范围里的字符
    print(re.search(r"r[0-9]n", "dog r2ns to cat"))        #匹配0-9范围里的字符
    print(re.search(r"r[0-9a-z]n", "dog runs to cat"))     #匹配0-9,a-z范围里的字符
    
    
    #特殊种类匹配
    
    #\d : 任何数字
    #\D : 不是数字
    #\s : 任何 white space, 如 [\t\n\r\f\v]
    #\S : 不是 white space
    #\w : 任何大小写字母, 数字和 “” [a-zA-Z0-9]
    #\W : 不是 \w
    #\b : 空白字符 (只在某个字的开头或结尾)
    #\B : 空白字符 (不在某个字的开头或结尾)
    #\\ : 匹配 \
    #. : 匹配任何字符 (除了 \n)
    #^ : 匹配开头
    #$ : 匹配结尾
    #? : 前面的字符可有可无
    
    #数字
    # \d : decimal digit
    print(re.search(r"r\dn", "run r4n"))         #
    # \D : any non-decimal digit
    print(re.search(r"r\Dn", "run r4n"))         #
    
    #空白
    # \s : any white space [\t\n\r\f\v]
    print(re.search(r"r\sn", "r\nn r4n"))               
    # \S : opposite to \s, any non-white space
    print(re.search(r"r\Sn", "r\nn r4n"))
    
    #所有字母数字和"_"
    # \w : [a-zA-Z0-9]
    print(re.search(r"r\wn", "r\nn r4n"))               
    # \W : opposite to \w
    print(re.search(r"r\Wn", "r\nn r4n"))
    
    #空白字符
    # \b : empty string (only at the start or end of the word)
    print(re.search(r"\bruns\b", "dog runs to cat"))    
    # \B : empty string (but not at the start or end of a word)
    print(re.search(r"\B runs \B", "dog   runs  to cat"))
    
    
    #特殊字符 任意字符
    # \\ : match \
    print(re.search(r"runs\\", "runs\ to me"))          
    # . : match anything (except \n)
    print(re.search(r"r.n", "r[ns to me"))
    
    
    #句尾句首
    # ^ : match line beginning
    print(re.search(r"^dog", "dog runs to cat"))        
    # $ : match line ending
    print(re.search(r"cat$", "dog runs to cat"))
    
    
    #是否
    # ? : may or may not occur
    print(re.search(r"Mon(day)?", "Monday"))            
    print(re.search(r"Mon(day)?", "Mon"))
    
    #多行匹配
    # multi-line
    string = """
    dog runs to cat.
    I run to dog.
    """
    print(re.search(r"^I", string))                     
    print(re.search(r"^I", string, flags=re.M))
    
    #0或多次
    # * : occur 0 or more times
    print(re.search(r"ab*", "a"))                       
    print(re.search(r"ab*", "abbbbb"))
    
    #1或多次
    # + : occur 1 or more times
    print(re.search(r"ab+", "a"))                       
    print(re.search(r"ab+", "abbbbb"))
    
    
    #可选次数
    # {n, m} : occur n to m times
    print(re.search(r"ab{2,10}", "a"))                  
    print(re.search(r"ab{2,10}", "abbbbb"))
    
    
    #group 组
    # group
    match = re.search(r"(\d+), Date: (.+)", "ID: 021523, Date: Feb/12/2017")
    print(match.group())                                
    print(match.group(1))                               
    print(match.group(2))
    
    
    match = re.search(r"(?P<id>\d+), Date: (?P<date>.+)", "ID: 021523, Date: Feb/12/2017")
    print(match.group('id'))                            
    print(match.group('date'))
    
    
    #寻找所有匹配
    # findall
    print(re.findall(r"r[ua]n", "run ran ren"))
    ['run', 'ran']
    
    # | : or
    print(re.findall(r"(run|ran)", "run ran ren"))
    ['run', 'ran']
    
    #替换
    # re.sub() replace
    print(re.sub(r"r[au]ns", "catches", "dog runs to cat"))
    
    
    #分裂re.split()
    print(re.split(r"[,;\.]", "a;b,c.d;e"))
    ['a', 'b', 'c', 'd', 'e']
    
    # compile
    compiled_re = re.compile(r"r[ua]n")
    print(compiled_re.search("dog ran to cat"))
本文为作者Urban-Ash发布,未经允许禁止转载!
67
0
0
发表留言

编程
Python - Re正则表达式
这里整理下Python中Re正则表达式的用法(☆▽☆)...
扫描右侧二维码继续阅读
June 17, 2020
Urban-AshのBlog
blogger
urban-ash
ようこそ - 我がBlogへ ! ! !
统计
文章:28 篇
分类:3 个
评论:13 条
运行时长:0年146天
by yoniu.

Urban-AshのBlog