<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

简介: re模块:核心函数和方法常见的正则表达式使用match() 方法匹配字符串 match是re模块函数和正则表达式对象方法。

re模块:核心函数和方法

  • 常见的正则表达式

  • 使用match() 方法匹配字符串

    • match是re模块函数和正则表达式对象方法。match函数试图从字符串的起始部分对模式进行匹配。如果匹配成功就返回一个匹配对象,如果匹配失败就返回None。

     
    import re
    m=re.match(‘foo’,’foo’) #模式匹配字符串
    m.group() #返回整个匹配对象
    Out[10]: ‘foo’
m=re.match(‘foo’,’bar’) #模式并不能匹配字符串 m.group() 这个就会出现报错了,跑出AttributeError异常,打印一下是None; re.match(‘foo’,’food on the table’).group() Out[13]: ‘foo’
  • 使用search()在一个字符串中查找模式(搜索与匹配的对比)

    • search函数在任意位置对给定正则表达式模式搜索第一次出现的匹配情况,如果搜索到成功的匹配,就会返回一个匹配对象,否则None.

     
    m=re.match(‘foo’,’seafood’)
    (m.groups() #匹配失败抛出异常

    re.search(‘foo’,’seafood’).group() #搜索成功返回第一次出现的相应字符串
    Out[18]: ‘foo’


  • 匹配多个字符串

    • 则一匹配 (|)符号
        bt='bat|bet|bit'     #正则表达式模式:bat、bet、bit
        m=re.match(bt,'bat') #'bat'是一个匹配
        m.group()
        Out[19]: 'bat'
        *****************************************
        bt='bat|bet|bit'     #正则表达式模式:bat、bet、bit
        m=re.match(bt,'blt') #'blt'没有匹配,抛出异常
        m.group()
        *****************************************
        bt='bat|bet|bit'     #正则表达式模式:bat、bet、bit
        m=re.match(bt,'he bit me!') #'bit'没有匹配,抛出异常
        m.group()
        *****************************************
        bt='bat|bet|bit'     #正则表达式模式:bat、bet、bit
        m=re.search(bt,'he bit me!') #'bit'被搜所到
        m.group()
        Out[22]: 'bit'
    

  • 匹配任何单个字符

    • 点号不能匹配一个换行符\n或者非字符(空字符串)。
       anyend='.end'
       re.match(anyend,'bend').group() #点好匹配'b'
       Out[24]: 'bend'
        *****************************************
        anyend='.end'
        re.match(anyend,'end').group() #不匹配任何字符,抛出异常
        *****************************************
        re.search('.end','The end.').group()
        Out[25]: ' end'
    

  • 创建字符集([])

        re.match('[cr][23][dp][o2]','c3po')  #匹配'c3po'
        Out[28]: 'c3po'
        *****************************************
       re.match('[cr][23][dp][o2]','c2do')#匹配'c2do'
        Out[29]: 'c2do'
        *****************************************
        re.match('r2d2|c3po','c2do').group()  #不匹配'c2do',抛出异常
    

  • 使用findall()和finditer()查找每一次出现的位置

    • findall()查询字符串中某个正则表达式模式全部的非重复出现情况,返回的是一个列表。
    • 和 findall 类似,在字符串中找到正则表达式所匹配的所有子串,并把它们作为一个迭代器返回。
     import  re
     re.findall('car','car')
     Out[4]: ['car']
     "*****************************************"
     re.findall('car','scary')
     Out[6]: ['car']
     "*****************************************"
     re.findall('car','carry the barcard to the car')
     Out[5]: ['car', 'car', 'car']
     "*****************************************"
    it =re.finditer(r'(th\w+) and (th\w+)',s,re.I)
    for match in it:
        print(match.groups())
    ('This', 'that')
    "*****************************************"
    match.group(1)
    Out[19]: 'This'
    "*****************************************"
    match.group(2)
    Out[20]: 'that'
    
  • 使用sub()和subn()搜索与替换

    • 两者几乎一样,都是将字符串中所有匹配正则表达式的部分进行某种形式的替换。不同的是,subn()还返回一个表是替换的总数。
    re.sub('X','Mr. Smith','attn:X\n\nDear X,\n')
    Out[33]: 'attn:Mr. Smith\n\nDear Mr. Smith,\n'
    "*****************************************"
    re.subn('X','Mr. Smith','attn:X\n\nDear X,\n')
    Out[34]: ('attn:Mr. Smith\n\nDear Mr. Smith,\n', 2)
    "*****************************************"
    print(re.sub('X','Mr. Smith','attn:X\n\nDear X,\n'))
    attn:Mr. Smith
    Dear Mr. Smith,
    "*****************************************"
    re.sub('[aa]','X','abcdef')
    Out[36]: 'Xbcdef'
    "*****************************************"
    re.subn('[aa]','X','abcdef')
    Out[37]: ('Xbcdef', 1)
    "*****************************************"
    re.sub(r'(\d{1,2})/(\d{1,2})/(\d{2}|\d{4})',r'\2/\1/\3','2/20/91')
    Out[30]: '20/2/91'
    "*****************************************"
    re.sub(r'(\d{1,2})/(\d{1,2})/(\d{2}|\d{4})',r'\2/\1/\3','2/20/1991')
    Out[31]: '20/2/1991'
    

  • 在限定模式上使用split()分割字符串

    • split()对于字符串的工作方式是类似的分割一个固定字符串相比,他们基于正则表达式的模式分割字符串。
    re.split(':','str1:Str2:str3')
    Out[42]: ['str1', 'Str2', 'str3']
    "*****************************************"
    DATA = ('Mountain View, CA 94040',
                'Sunnyvale, CA',
                'Los Altos, 94023',
                'Cupertino 95014',
                'Palo Alto CA', )
    for datum in DATA:
    print(re.split(', |(?= (?:\d{5}|[A-Z]{2})) ',datum))
    ['Mountain View', 'CA', '94040']
    ['Sunnyvale', 'CA']
    ['Los Altos', '94023']
    ['Cupertino', '95014']
    ['Palo Alto', 'CA']
    
目录
相关文章
|
Web App开发 前端开发 Android开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
使用MAT分析内存泄露 对于大型服务端应用程序来说,有些内存泄露问题很难在测试阶段发现,此时就需要分析JVM Heap Dump文件来找出问题。
1075 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
系统的升级涉及各个架构组件,细节很多。常年累月的修修补补使老系统积累了很多问题。 系统升级则意味着需要repair之前埋下的雷,那为何还要升级,可以考虑以下几个方面 成熟老系统常见问题: 1. 缺乏文档(这应该是大小公司都存在的问题。
847 0
|
Web App开发 前端开发
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
PipeMapRed.waitOutputThreads(): subprocess failed with code X ,这里code X对应的信息如下:error code 1: Operation not perm...
1265 0
|
Web App开发 前端开发 算法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
基于大数据的精准营销与应用场景 2015年08月11日 大数据 大数据营销时代来临营销学领域过去半个多世纪的发展让我们见证了从“以产品为中心”到“以客户为中心”的转变。
1210 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
service cloudera-scm-agent stop service cloudera-scm-agent stop umount /var/run/cloudera-scm-agent/process umo...
980 0
|
SQL Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
     如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则在出现重复值的行执行UPDATE;如果不会导致唯一值列重复的问题,则插入新行。
1059 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
使用hive分析日志作业很多的时候,需要修改mysql的默认连接数 修改方法   打开/etc/my.cnf文件 在[mysqld]  中添加 max_connections=1000 重启mysql服务  service mysqld restart mysql>show variables like '%max_connections%'; 查看当前mysql的连接数方法 mysqladmin -uroot -p status 其中,Uptime:mysqld运行时间,单位秒。
881 0
|
Web App开发 前端开发
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
1.使用lsmod查看ipv6的模块是否被加载。 lsmod | grep ipv6 [root@dmhadoop011 ~]# lsmod | grep ipv6 ipv6                  317340  127 bonding 如果加载了,则进行如下操作: 2.
1024 0

热门文章

最新文章