python正文(两)

 在本文中,我读了记录和总结《Python标准库》一本书,本节课文的学习和理解。




事实上,在Python于,使用一些方法这段文字是一回事,尤其是经常使用。在一般情况下,会用String这样的类,应考虑Python个标准类了。

1.3.6 用组解析匹配

match.groups()会依照表达式中与字符串匹配的组的顺序返回一个字符串序列。

使用group()能够得到某个组的匹配。

    #组解析
    text='This is a text -- with punctuation.'
    
    print 'Input text:  ', text
    
    regex=re.compile(r'(\bt\w+)\W+(\w+)')
    
    print 'pattern:  ', regex.pattern
    
    match=regex.search(text)
     
    print 'Entire match: ',match.group(0)
    print 'Word starting with t: ',match.group(1)
    print 'Word after t word: ',match.group(2)

Python对基本分组的语法进行了拓展,添加了命名组(named group)。通过名字来指示组。方便能够更easy的改动模式,而不必同一时候改动使用了该匹配结果的代码。
语法:(?

P<name>pattern)

    #命名组
    print '-'*30
    for pattern in [r'^(?

P<first_word>\w+)', r'(?P<last_word>\w+)\S*$', r'(?

P<t_word>\bt\w+)\W+(?P<other_word>\w+)', r'(?P<ends_with_t>\w+t)\b' ]: regex=re.compile(pattern) match=regex.search(text) print 'Matching "%s"' % pattern print ' ',match.groups() print ' ',match.groupdict() print '\n'


使用groupdict()能够获取一个字典,它将组名映射到匹配的子串。



    #更新后的test_pattern()
    print '-'*30
    def test_pattern(text, patterns=[]):
    """
    Given the source text and a list of patters, 
    look for matches for each pattern within the text and print them to stdout.
    """    
    #look for each pattern in the text and print the results
    for pattern, desc in patterns:
        print 'pattern %r (%s) \n' %(pattern, desc)
        print '%r' % text
        for match in re.finditer(pattern,text):
            s=match.start()
            e=match.end()
            prefix=' '*(s)
            print '  %s%r%s' % (prefix,text[s:e],' '*(len(text)-e))
            print match.groups()
            if match.groupdict():
                print '%s%s'%(' '*(len(text)-s),match.groupdict())
        print 
    return

    test_pattern(
             'abbaabbba',
             [ (r'a((a*)(b*))','a followed by 0-n a and 0-n b'),]
             )



版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-10-18 19:34  mfrbuaa  阅读(192)  评论(0编辑  收藏  举报