zoukankan      html  css  js  c++  java
  • day5-正则表达式 re

    re模块用于对python的正则表达式的操作。

    字符:

      . 匹配除换行符以外的任意字符
      w 匹配字母或数字或下划线或汉字
      s 匹配任意的空白符
      d 匹配数字
       匹配单词的开始或结束
      ^ 匹配字符串的开始
      $ 匹配字符串的结束

    次数:

      * 重复零次或更多次
      + 重复一次或更多次
      ? 重复零次或一次
      {n} 重复n次
      {n,} 重复n次或更多次
      {n,m} 重复n到m次

    IP:
    ^(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}$
    手机号:
    ^1[3|4|5|8][0-9]d{8}$

    1、match(pattern, string, flags=0)

    从起始位置开始根据模型去字符串中匹配指定内容,匹配单个

    • 正则表达式
    • 要匹配的字符串
    • 标志位,用于控制正则表达式的匹配方式
    import re
    
    obj = re.match('d+', '123uuasf')
    if obj:
        print obj.group()
    # flags
    I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
    L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
    U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode locale
    M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
    S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline
    X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments

    2、search(pattern, string, flags=0)

    根据模型去字符串中匹配指定内容,匹配单个

    import re
    
    obj = re.search('d+', 'u123uu888asf')
    if obj:
        print obj.group()

    3、group和groups

    复制代码
    a = "123abc456"print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group()
    
    print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0)
    print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1)
    print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2)
    
    print re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups()
    复制代码

    4、findall(pattern, string, flags=0)

    上述两中方式均用于匹配单值,即:只能匹配字符串中的一个,如果想要匹配到字符串中所有符合条件的元素,则需要使用 findall。

    import re
    
    obj = re.findall('d+', 'fa123uu888asf')
    print obj

    5、sub(pattern, repl, string, count=0, flags=0)

    用于替换匹配的字符串

    content = "123abc456"
    new_content = re.sub('d+', 'sb', content)
    # new_content = re.sub('d+', 'sb', content, 1)print new_content

    相比于str.replace功能更加强大

    6、split(pattern, string, maxsplit=0, flags=0)

    根据指定匹配进行分组

    content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"
    new_content = re.split('*', content)
    # new_content = re.split('*', content, 1)print new_content
    content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"
    new_content = re.split('[+-*/]+', content)
    # new_content = re.split('*', content, 1)print new_content
    inpp = '1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))'
    inpp = re.sub('s*','',inpp)
    new_content = re.split('(([+-*/]?d+[+-*/]?d+){1})', inpp, 1)
    print new_content

    相比于str.split更加强大  

    7、分组匹配地址

    1. aaa = "111,222,333"
    2. bbb = re.search(r'(d+,)(d+),(d+)',aaa)
    3. print(bbb.group(1))
    4. print(bbb.group(2))
    5. print(bbb.group(3))
    6. 111,
    7. 222
    8. 333

    匹配IP地址
    1. str = "192.168.1.1"
    2. m = re.match("([0-9]{1,3}.?){4}",str).group()
    3. print(m)
    4. 192.168.1.1

    re.match与re.search的区别

    re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。


    其它例子,匹配除了某某之外的

    1. aaa = "www.m.biyao.com"
    2. bbb = re.search("[^.]+.",aaa).group()
    3. # 会匹配出 www. 因为规则是匹配除了.之外的一个或多个值加上一个.
    4. bbb = re.search("[^b]+",aaa).group()
    5. # 会匹配出 www.m. 因为规则是匹配除了b之外的一个或多个值.
    6. print(bbb)






  • 相关阅读:
    开发者论坛一周精粹(第九期)
    你刚吃的兰州牛肉面_背后就藏着大数据
    《C++覆辙录》——1.9:使用糟糕的语言
    老司机带你用MaxCompute和表格存储玩转车联网数据
    Gartner最新发布:2017年十大战略技术趋势
    js的事件的三个阶段,事件委托的原理
    Spring的AOP1
    了解SQL注入攻击
    了解XSS攻击
    了解Serialization
  • 原文地址:https://www.cnblogs.com/bruceg/p/5199102.html
Copyright © 2011-2022 走看看