PyXR

c:\python24\lib \ test \ test_regex.py



0001 from test.test_support import verbose, sortdict
0002 import warnings
0003 warnings.filterwarnings("ignore", "the regex module is deprecated",
0004                         DeprecationWarning, __name__)
0005 import regex
0006 from regex_syntax import *
0007 
0008 re = 'a+b+c+'
0009 print 'no match:', regex.match(re, 'hello aaaabcccc world')
0010 print 'successful search:', regex.search(re, 'hello aaaabcccc world')
0011 try:
0012     cre = regex.compile('\(' + re)
0013 except regex.error:
0014     print 'caught expected exception'
0015 else:
0016     print 'expected regex.error not raised'
0017 
0018 print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
0019 prev = regex.set_syntax(RE_SYNTAX_AWK)
0020 print 'successful awk syntax:', regex.search('(a+)|(b+)', 'cdb')
0021 regex.set_syntax(prev)
0022 print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
0023 
0024 re = '\(<one>[0-9]+\) *\(<two>[0-9]+\)'
0025 print 'matching with group names and compile()'
0026 cre = regex.compile(re)
0027 print cre.match('801 999')
0028 try:
0029     print cre.group('one')
0030 except regex.error:
0031     print 'caught expected exception'
0032 else:
0033     print 'expected regex.error not raised'
0034 
0035 print 'matching with group names and symcomp()'
0036 cre = regex.symcomp(re)
0037 print cre.match('801 999')
0038 print cre.group(0)
0039 print cre.group('one')
0040 print cre.group(1, 2)
0041 print cre.group('one', 'two')
0042 print 'realpat:', cre.realpat
0043 print 'groupindex:', sortdict(cre.groupindex)
0044 
0045 re = 'world'
0046 cre = regex.compile(re)
0047 print 'not case folded search:', cre.search('HELLO WORLD')
0048 cre = regex.compile(re, regex.casefold)
0049 print 'case folded search:', cre.search('HELLO WORLD')
0050 
0051 print '__members__:', cre.__members__
0052 print 'regs:', cre.regs
0053 print 'last:', cre.last
0054 print 'translate:', len(cre.translate)
0055 print 'givenpat:', cre.givenpat
0056 
0057 print 'match with pos:', cre.match('hello world', 7)
0058 print 'search with pos:', cre.search('hello world there world', 7)
0059 print 'bogus group:', cre.group(0, 1, 3)
0060 try:
0061     print 'no name:', cre.group('one')
0062 except regex.error:
0063     print 'caught expected exception'
0064 else:
0065     print 'expected regex.error not raised'
0066 
0067 from regex_tests import *
0068 if verbose: print 'Running regex_tests test suite'
0069 
0070 for t in tests:
0071     pattern=s=outcome=repl=expected=None
0072     if len(t)==5:
0073         pattern, s, outcome, repl, expected = t
0074     elif len(t)==3:
0075         pattern, s, outcome = t
0076     else:
0077         raise ValueError, ('Test tuples should have 3 or 5 fields',t)
0078 
0079     try:
0080         obj=regex.compile(pattern)
0081     except regex.error:
0082         if outcome==SYNTAX_ERROR: pass    # Expected a syntax error
0083         else:
0084             # Regex syntax errors aren't yet reported, so for
0085             # the official test suite they'll be quietly ignored.
0086             pass
0087             #print '=== Syntax error:', t
0088     else:
0089         try:
0090             result=obj.search(s)
0091         except regex.error, msg:
0092             print '=== Unexpected exception', t, repr(msg)
0093         if outcome==SYNTAX_ERROR:
0094             # This should have been a syntax error; forget it.
0095             pass
0096         elif outcome==FAIL:
0097             if result==-1: pass   # No match, as expected
0098             else: print '=== Succeeded incorrectly', t
0099         elif outcome==SUCCEED:
0100             if result!=-1:
0101                 # Matched, as expected, so now we compute the
0102                 # result string and compare it to our expected result.
0103                 start, end = obj.regs[0]
0104                 found=s[start:end]
0105                 groups=obj.group(1,2,3,4,5,6,7,8,9,10)
0106                 vardict=vars()
0107                 for i in range(len(groups)):
0108                     vardict['g'+str(i+1)]=str(groups[i])
0109                 repl=eval(repl)
0110                 if repl!=expected:
0111                     print '=== grouping error', t, repr(repl)+' should be '+repr(expected)
0112             else:
0113                 print '=== Failed incorrectly', t
0114 

Generated by PyXR 0.9.4
SourceForge.net Logo