blob: 2e2c8f65a12c1e00fa669e8d8a1324a08b1aebbf [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test.test_support import verbose, sortdict
Fred Drake9a1a7dd2000-12-23 22:08:27 +00002import warnings
3warnings.filterwarnings("ignore", "the regex module is deprecated",
Fred Drake1e146e72002-10-17 22:13:28 +00004 DeprecationWarning, __name__)
Barry Warsaw4b722781996-12-20 22:00:21 +00005import regex
6from regex_syntax import *
7
8re = 'a+b+c+'
9print 'no match:', regex.match(re, 'hello aaaabcccc world')
10print 'successful search:', regex.search(re, 'hello aaaabcccc world')
11try:
12 cre = regex.compile('\(' + re)
13except regex.error:
14 print 'caught expected exception'
15else:
16 print 'expected regex.error not raised'
17
18print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
19prev = regex.set_syntax(RE_SYNTAX_AWK)
20print 'successful awk syntax:', regex.search('(a+)|(b+)', 'cdb')
21regex.set_syntax(prev)
22print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
23
24re = '\(<one>[0-9]+\) *\(<two>[0-9]+\)'
25print 'matching with group names and compile()'
26cre = regex.compile(re)
27print cre.match('801 999')
28try:
29 print cre.group('one')
30except regex.error:
31 print 'caught expected exception'
32else:
33 print 'expected regex.error not raised'
34
35print 'matching with group names and symcomp()'
36cre = regex.symcomp(re)
37print cre.match('801 999')
38print cre.group(0)
39print cre.group('one')
40print cre.group(1, 2)
41print cre.group('one', 'two')
42print 'realpat:', cre.realpat
Tim Peters2f228e72001-05-13 00:19:31 +000043print 'groupindex:', sortdict(cre.groupindex)
Barry Warsaw4b722781996-12-20 22:00:21 +000044
45re = 'world'
46cre = regex.compile(re)
47print 'not case folded search:', cre.search('HELLO WORLD')
48cre = regex.compile(re, regex.casefold)
49print 'case folded search:', cre.search('HELLO WORLD')
50
51print '__members__:', cre.__members__
52print 'regs:', cre.regs
53print 'last:', cre.last
Guido van Rossum3f11da01997-05-16 13:51:48 +000054print 'translate:', len(cre.translate)
Barry Warsaw4b722781996-12-20 22:00:21 +000055print 'givenpat:', cre.givenpat
56
57print 'match with pos:', cre.match('hello world', 7)
58print 'search with pos:', cre.search('hello world there world', 7)
59print 'bogus group:', cre.group(0, 1, 3)
60try:
61 print 'no name:', cre.group('one')
62except regex.error:
63 print 'caught expected exception'
64else:
65 print 'expected regex.error not raised'
Guido van Rossum876736c1997-06-03 18:07:49 +000066
67from regex_tests import *
68if verbose: print 'Running regex_tests test suite'
69
70for t in tests:
71 pattern=s=outcome=repl=expected=None
72 if len(t)==5:
Guido van Rossum41360a41998-03-26 19:42:58 +000073 pattern, s, outcome, repl, expected = t
Guido van Rossum876736c1997-06-03 18:07:49 +000074 elif len(t)==3:
Fred Drake004d5e62000-10-23 17:22:08 +000075 pattern, s, outcome = t
Guido van Rossum876736c1997-06-03 18:07:49 +000076 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000077 raise ValueError, ('Test tuples should have 3 or 5 fields',t)
Guido van Rossum876736c1997-06-03 18:07:49 +000078
79 try:
Guido van Rossum41360a41998-03-26 19:42:58 +000080 obj=regex.compile(pattern)
Guido van Rossum876736c1997-06-03 18:07:49 +000081 except regex.error:
Guido van Rossum41360a41998-03-26 19:42:58 +000082 if outcome==SYNTAX_ERROR: pass # Expected a syntax error
Fred Drake004d5e62000-10-23 17:22:08 +000083 else:
84 # Regex syntax errors aren't yet reported, so for
Guido van Rossum41360a41998-03-26 19:42:58 +000085 # the official test suite they'll be quietly ignored.
86 pass
87 #print '=== Syntax error:', t
Guido van Rossum876736c1997-06-03 18:07:49 +000088 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000089 try:
90 result=obj.search(s)
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.
95 pass
96 elif outcome==FAIL:
97 if result==-1: pass # No match, as expected
98 else: print '=== Succeeded incorrectly', t
99 elif outcome==SUCCEED:
100 if result!=-1:
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]
104 found=s[start:end]
105 groups=obj.group(1,2,3,4,5,6,7,8,9,10)
106 vardict=vars()
107 for i in range(len(groups)):
108 vardict['g'+str(i+1)]=str(groups[i])
109 repl=eval(repl)
110 if repl!=expected:
111 print '=== grouping error', t, repr(repl)+' should be '+repr(expected)
112 else:
113 print '=== Failed incorrectly', t