1 from test_support
import verbose
3 warnings
.filterwarnings("ignore", "the regex module is deprecated",
4 DeprecationWarning, __name__
)
6 from regex_syntax
import *
9 print 'no match:', regex
.match(re
, 'hello aaaabcccc world')
10 print 'successful search:', regex
.search(re
, 'hello aaaabcccc world')
12 cre
= regex
.compile('\(' + re
)
14 print 'caught expected exception'
16 print 'expected regex.error not raised'
18 print 'failed awk syntax:', regex
.search('(a+)|(b+)', 'cdb')
19 prev
= regex
.set_syntax(RE_SYNTAX_AWK
)
20 print 'successful awk syntax:', regex
.search('(a+)|(b+)', 'cdb')
21 regex
.set_syntax(prev
)
22 print 'failed awk syntax:', regex
.search('(a+)|(b+)', 'cdb')
24 re
= '\(<one>[0-9]+\) *\(<two>[0-9]+\)'
25 print 'matching with group names and compile()'
26 cre
= regex
.compile(re
)
27 print cre
.match('801 999')
29 print cre
.group('one')
31 print 'caught expected exception'
33 print 'expected regex.error not raised'
35 print 'matching with group names and symcomp()'
36 cre
= regex
.symcomp(re
)
37 print cre
.match('801 999')
39 print cre
.group('one')
41 print cre
.group('one', 'two')
42 print 'realpat:', cre
.realpat
43 print 'groupindex:', cre
.groupindex
46 cre
= regex
.compile(re
)
47 print 'not case folded search:', cre
.search('HELLO WORLD')
48 cre
= regex
.compile(re
, regex
.casefold
)
49 print 'case folded search:', cre
.search('HELLO WORLD')
51 print '__members__:', cre
.__members
__
52 print 'regs:', cre
.regs
53 print 'last:', cre
.last
54 print 'translate:', len(cre
.translate
)
55 print 'givenpat:', cre
.givenpat
57 print 'match with pos:', cre
.match('hello world', 7)
58 print 'search with pos:', cre
.search('hello world there world', 7)
59 print 'bogus group:', cre
.group(0, 1, 3)
61 print 'no name:', cre
.group('one')
63 print 'caught expected exception'
65 print 'expected regex.error not raised'
67 from regex_tests
import *
68 if verbose
: print 'Running regex_tests test suite'
71 pattern
=s
=outcome
=repl
=expected
=None
73 pattern
, s
, outcome
, repl
, expected
= t
75 pattern
, s
, outcome
= t
77 raise ValueError, ('Test tuples should have 3 or 5 fields',t
)
80 obj
=regex
.compile(pattern
)
82 if outcome
==SYNTAX_ERROR
: pass # Expected a syntax error
84 # Regex syntax errors aren't yet reported, so for
85 # the official test suite they'll be quietly ignored.
87 #print '=== Syntax error:', t
91 except regex
.error
, msg
:
92 print '=== Unexpected exception', t
, repr(msg
)
93 if outcome
==SYNTAX_ERROR
:
94 # This should have been a syntax error; forget it.
97 if result
==-1: pass # No match, as expected
98 else: print '=== Succeeded incorrectly', t
99 elif outcome
==SUCCEED
:
101 # Matched, as expected, so now we compute the
102 # result string and compare it to our expected result.
103 start
, end
= obj
.regs
[0]
105 groups
=obj
.group(1,2,3,4,5,6,7,8,9,10)
107 for i
in range(len(groups
)):
108 vardict
['g'+str(i
+1)]=str(groups
[i
])
111 print '=== grouping error', t
, repr(repl
)+' should be '+repr(expected
)
113 print '=== Failed incorrectly', t