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

    1、
    re.match(pattern, string, flags=0)
    re.search(pattern, string, flags=0)    
    pattern:正则表达式
    string:要匹配的字符串
    flags:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等

    标志位:
     re.I | re.M
     re.I:对大小写不敏感
     re.M:多行匹配,影响 ^ 和 $

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

    group()用来提取分组截获的字符串,()用来分组

    import re
    a = "123abc456"
    print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(0)   #123abc456,返回整体
    print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(1)   #123
    print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(2)   #abc
    print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(3)   #456


    2、
    re.sub(pattern, repl, string, count=0)    #替换
    pattern:正则表达式
    repl:替换的部分
    string:被替换的部分
    count:是模式匹配后替换的最大次数;count 必须是非负整数。
               缺省值是 0 表示替换所有的匹配。(可不写)

    一些正则表达式模式

  • 相关阅读:
    Swift
    美国绿卡
    H-1-B签证简介
    托福、雅思和GRE的区别
    使用fdisk命令对linux硬盘进行操作
    Welcome to Workrave
    Installing patches on an ESXi 5.x by the command
    How to install VIB on VMware ESXi
    Robocopy用法
    Location of ESXi 5.1 log files
  • 原文地址:https://www.cnblogs.com/stellar/p/5976926.html
Copyright © 2011-2022 走看看