#组解析 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)
#命名组 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'
#更新后的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'),] )
原文地址:http://blog.csdn.net/ling1510/article/details/40506205