zoukankan      html  css  js  c++  java
  • phython正则表达式 Python Re模块

    反斜杠问题

    与大多数编程语言相同,正则表达式里使用””作为转义字符,这就可能造成反斜杠困扰。假如你需要匹配文本中的字符””,

    Python里的原生字符串很好地解决了这个问题,这个例子中的正则表达式可以使用r”\”表示。同样,匹配一个数字的”\d”可以写成r”d”。有了原生字符串,妈妈也不用担心是不是漏写了反斜杠,写出来的表达式也更直观勒。

    获得这个匹配模式:需要利用re.compile方法就可以

    __author__ = 'CQC'
    # -*- coding: utf-8 -*-
     
    #导入re模块
    import re
     
    # 将正则表达式编译成Pattern对象,注意hello前面的r的意思是“原生字符串”
    pattern = re.compile(r'hello')
     
    # 使用re.match匹配文本,获得匹配结果,无法匹配时将返回None
    result1 = re.match(pattern,'hello')
    result2 = re.match(pattern,'helloo CQC!')
    result3 = re.match(pattern,'helo CQC!')
    result4 = re.match(pattern,'hello CQC!')
     
    #如果1匹配成功
    if result1:
        # 使用Match获得分组信息
        print result1.group()//默认为组0
    else:
        print '1匹配失败!'
     
     
    #如果2匹配成功
    if result2:
        # 使用Match获得分组信息
        print result2.group()
    else:
        print '2匹配失败!'
     
     
    #如果3匹配成功
    if result3:
        # 使用Match获得分组信息
        print result3.group()
    else:
        print '3匹配失败!'
     
    #如果4匹配成功
    if result4:
        # 使用Match获得分组信息
        print result4.group()
    else:
        print '4匹配失败!'

    属性:
    1.string: 匹配时使用的文本  是原来的文本~

    2.re: 匹配时使用的Pattern对象。

    4.endpos: 文本中正则表达式结束搜索的索引。值与Pattern.match()和Pattern.seach()方法的同名参数相同。
    5.lastindex: 最后一个被捕获的分组在文本中的索引。如果没有被捕获的分组,将为None。

    # 匹配如下内容:单词+空格+单词+任意字符

    m = re.match(r'(w+) (w+)(?P<sign>.*)', 'hello world!')
    三个分组
    w+所有字母  hello
    2  w+所有字母 world 
    3 分组名字为sign  内容为任意字符的*所有(集合)

    match()函数只检测re是不是在string的开始位置匹配,search()会扫描整个string查找匹配,

    match = re.match(pattern,'hello world!')
    if match:
        # 使用Match获得分组信息
        print match.group()
    else:print "NO"
    ### 输出 ###
    # world
    match = re.search(pattern,'hello world!')
    if match:
        # 使用Match获得分组信息
        print match.group()
    else:print "NO"
    
    match 和 saerch 区别

    3)re.split(pattern, string[, maxsplit])

    以数字(可以多个数字)为分割 ,maxsplit=2 表示分割两次(不是两部分)指的是用“数字”分割2次(这里有4个数字)

    pattern = re.compile(r'd+')
    print re.split(pattern,'one1two2three3four4')
    
    ### 输出 ###
    # ['one', 'two', 'three', 'four', '']
    
    
    
    pattern = re.compile(r'd+')
    print re.split(pattern,'one1two2three3four4',2)
    
    ### 输出 ###
    # ['one', 'two', 'threefour']
    
    re.split(pattern, string[, maxsplit])

    (4)re.findall(pattern, string[, flags])见名思议 find all match 找出所有匹配的,这个应该是很常用的语句吧~

     pattern = re.compile(r'W+')
    2 print re.findall(pattern, '/one1two*2three3four4!')
    3 
    4 ### 输出 ###
    5 # ['/', '*', '!']

    w:代表字母数字下划线.

    (6)re.sub(pattern, repl, string[, count])

    对匹配的文本 用repl 替换  这里的2(id)代表的是第二个分组   0分组是整个文本!!!第一个左括号是第一个分组

    pattern = re.compile(r'(w+) (w+)')
    s = 'i say, hello world!'<br>你看这里的第一个分组就是i<br>第二个分组是say
        
    print re.sub(pattern,r'2 1', s)<br>用say i替换i say<br><br>

    在这个例子里的 func函数用的挺巧妙

    .title是使得第一字母大写?~~?!

    import re
     
    pattern = re.compile(r'(w+) (w+)')
    s = 'i say, hello world!'
     
    print re.sub(pattern,r'2 1', s)
     
    def func(m):
        return m.group(1)<span style="color: #ff6600;">.title()</span> + ' ' + m.group(2).title()
     
    print re.sub(pattern,func, s)
     
    ### output ###
    # say i, world hello!
    # I Say, Hello World!

    re.subn(pattern, repl, string[, count])

    只是输出结果中有一个替换次数

    输出结果对比

    say i, world hello!
    I Say, Hello World!
    ('say i, world hello!', 2)
    ('I Say, Hello World!', 2)

  • 相关阅读:
    ACM ICPC 2008–2009 NEERC MSC A, B, C, G, L
    POJ 1088 滑雪 DP
    UVA 11584 最短回文串划分 DP
    POJ 2531 Network Saboteur DFS+剪枝
    UVa 10739 String to Palindrome 字符串dp
    UVa 11151 Longest Palindrome 字符串dp
    UVa 10154 Weights and Measures dp 降维
    UVa 10271 Chopsticks dp
    UVa 10617 Again Palindrome 字符串dp
    UVa 10651 Pebble Solitaire 状态压缩 dp
  • 原文地址:https://www.cnblogs.com/wuxl360/p/5564292.html
Copyright © 2011-2022 走看看