#!/usr/bin/env python import pcre from pprint import pprint pattern = r'(?<sna>fo{2})b' subject = 'foobar FoObaz' options = pcre.PCRE_CASELESS compiled = pcre.pcre_compile(pattern, options) extra = pcre.pcre_study(compiled) result = pcre.pcre_exec(compiled, subject, extra=extra) # find the first match print('%d matches:' % result.num_matches) for i in xrange(result.num_matches): print(' "%s"' % repr(result.matches[i])) print('named substrings:') pprint(result.named_matches) # find all the matches results = pcre.pcre_find_all(compiled, subject, extra=extra) print '*** find all ***' for result in results: for i in xrange(result.num_matches): print(' "%s"' % repr(result.matches[i]))
import string import sys sys.path.append('..') import pcre import ErrorHandler import SkunkExcept from DTExcept import DTLexicalError #regex for a quoted string (no backslach escaping) quotedStr = "('(.*?)' | \"(.*?)\" | `(.*?)`)" #plain thing plain = '[^\s="\'`]+' plainre = pcre.pcre_compile(plain, 0, {}) #regex for parsing contents of a tag idxdict = {} tagContents = '''( (?P<plain> %(plain)s ) (?=($|\s)) | (?P<plaineq> %(plain)s ) \s* = \s* (?P<plaineqval> %(plain)s ) (?=($|\s)) | (?P<plaineqq> %(plain)s ) \s* = \s* (?P<plaineqqval> %(quotedStr)s ) (?=($|\s)) | (?P<quotonly> %(quotedStr)s (?=($|\s))) | (?P<whitespace> \s) )''' % { 'plain': plain, 'quotedStr': quotedStr }
import string import sys sys.path.append('..') import pcre import ErrorHandler import SkunkExcept from DTExcept import DTLexicalError #regex for a quoted string (no backslach escaping) quotedStr="('(.*?)' | \"(.*?)\" | `(.*?)`)" #plain thing plain='[^\s="\'`]+' plainre=pcre.pcre_compile(plain,0,{}) #regex for parsing contents of a tag idxdict={} tagContents='''( (?P<plain> %(plain)s ) (?=($|\s)) | (?P<plaineq> %(plain)s ) \s* = \s* (?P<plaineqval> %(plain)s ) (?=($|\s)) | (?P<plaineqq> %(plain)s ) \s* = \s* (?P<plaineqqval> %(quotedStr)s ) (?=($|\s)) | (?P<quotonly> %(quotedStr)s (?=($|\s))) | (?P<whitespace> \s) )''' % {'plain': plain, 'quotedStr': quotedStr } tagContentsRe=pcre.pcre_compile(