@@ -51,13 +51,13 @@ def search(pattern, string, flags=0):
5151 return compile (pattern , flags ).search (string )
5252
5353def sub (pattern , repl , string , count = 0 ):
54- pass
54+ return compile ( pattern ). sub ( repl , string , count )
5555
5656def subn (pattern , repl , string , count = 0 ):
57- pass
57+ return compile ( pattern ). subn ( repl , string , count )
5858
59- def split (string , pattern , maxsplit = 0 ):
60- pass
59+ def split (pattern , string , maxsplit = 0 ):
60+ return compile ( pattern ). subn ( string , maxsplit )
6161
6262#
6363#
@@ -79,21 +79,6 @@ def __init__(self, pattern, flags, code, num_regs, groupindex, callouts):
7979 else :
8080 self .anchor = 0
8181 self .buffer = assemble (code )
82- def match (self , string , pos = 0 ):
83- regs = reop .match (self .buffer ,
84- self .num_regs ,
85- self .flags ,
86- self .fastmap .can_be_null ,
87- self .fastmap .fastmap (),
88- self .anchor ,
89- string ,
90- pos )
91- if regs is None :
92- return None
93- return MatchObject (self ,
94- string ,
95- pos ,
96- regs )
9782 def search (self , string , pos = 0 ):
9883 regs = reop .search (self .buffer ,
9984 self .num_regs ,
@@ -109,41 +94,77 @@ def search(self, string, pos=0):
10994 string ,
11095 pos ,
11196 regs )
97+ def match (self , string , pos = 0 ):
98+ regs = reop .match (self .buffer ,
99+ self .num_regs ,
100+ self .flags ,
101+ self .fastmap .can_be_null ,
102+ self .fastmap .fastmap (),
103+ self .anchor ,
104+ string ,
105+ pos )
106+ if regs is None :
107+ return None
108+ return MatchObject (self ,
109+ string ,
110+ pos ,
111+ regs )
112+ def sub (self , repl , string , count = 0 ):
113+ pass
114+ def subn (self , repl , string , count = 0 ):
115+ pass
116+ def split (self , string , maxsplit = 0 ):
117+ pass
112118
113119class MatchObject :
114120 def __init__ (self , re , string , pos , regs ):
115121 self .re = re
116122 self .string = string
117123 self .pos = pos
118124 self .regs = regs
119- def start (self , i ):
120- if type (i ) == type ('' ):
125+ def start (self , g ):
126+ if type (g ) == type ('' ):
121127 try :
122- i = self .re .groupindex [i ]
128+ g = self .re .groupindex [g ]
123129 except (KeyError , TypeError ):
124- raise IndexError
125- return self .regs [i ][0 ]
126- def end (self , i ):
127- if type (i ) == type ('' ):
130+ raise IndexError , ( 'group "' + g + '" is undefined' )
131+ return self .regs [g ][0 ]
132+ def end (self , g ):
133+ if type (g ) == type ('' ):
128134 try :
129- i = self .re .groupindex [i ]
135+ g = self .re .groupindex [g ]
130136 except (KeyError , TypeError ):
131- raise IndexError
132- return self .regs [i ][1 ]
133- def span (self , i ):
134- if type (i ) == type ('' ):
137+ raise IndexError , ( 'group "' + g + '" is undefined' )
138+ return self .regs [g ][1 ]
139+ def span (self , g ):
140+ if type (g ) == type ('' ):
135141 try :
136- i = self .re .groupindex [i ]
142+ g = self .re .groupindex [g ]
137143 except (KeyError , TypeError ):
138- raise IndexError
139- return self .regs [i ]
140- def group (self , i ):
141- if type (i ) == type ('' ):
142- try :
143- i = self .re .groupindex [i ]
144- except (KeyError , TypeError ):
145- raise IndexError
146- return self .string [self .regs [i ][0 ]:self .regs [i ][1 ]]
144+ raise IndexError , ('group "' + g + '" is undefined' )
145+ return self .regs [g ]
146+ def group (self , * groups ):
147+ if len (groups ) == 0 :
148+ groups = range (1 , self .re .num_regs )
149+ result = []
150+ for g in groups :
151+ if type (g ) == type ('' ):
152+ try :
153+ g = self .re .groupindex [g ]
154+ except (KeyError , TypeError ):
155+ raise IndexError , ('group "' + g + '" is undefined' )
156+ if g >= len (self .regs ):
157+ result .append (None )
158+ elif (self .regs [g ][0 ] == - 1 ) or (self .regs [g ][1 ] == - 1 ):
159+ result .append (None )
160+ else :
161+ result .append (self .string [self .regs [g ][0 ]:self .regs [g ][1 ]])
162+ if len (result ) > 1 :
163+ return tuple (result )
164+ elif len (result ) == 1 :
165+ return result [0 ]
166+ else :
167+ return ()
147168
148169#
149170# A set of classes to make assembly a bit easier, if a bit verbose.
@@ -331,7 +352,7 @@ def __init__(self, syntax):
331352 def assemble (self , postition , labels ):
332353 return self .opcode + chr (self .syntax )
333354
334- class SyntaxSpec (Instruction ):
355+ class NotSyntaxSpec (Instruction ):
335356 name = 'notsyntaxspec'
336357 def __init__ (self , syntax ):
337358 self .syntax = syntax
@@ -382,13 +403,12 @@ def assemble(instructions):
382403#
383404
384405def escape (pattern ):
385- result = ''
406+ result = []
386407 for char in pattern :
387408 if 'word' not in syntax_table [char ]:
388- result = result + '\\ ' + char
389- else :
390- result = result + char
391- return result
409+ result .append ('\\ ' )
410+ result .append (char )
411+ return string .join (result , '' )
392412
393413#
394414#
@@ -631,7 +651,7 @@ def compile(pattern, flags=0):
631651 expr + \
632652 [Jump (- 1 ),
633653 Label (label )])
634- stack .append ([( '|' , )])
654+ stack .append ([Alternation ( )])
635655 label = label + 1
636656
637657 elif char == '(' :
@@ -693,12 +713,13 @@ def compile(pattern, flags=0):
693713 stack .append ([FunctionCallout (name )])
694714
695715 else :
696- raise error , 'unknown Python extension'
716+ raise error , ('unknown Python extension: ' + \
717+ pattern [index ])
697718
698719 elif pattern [index ] == ':' :
699720 # grouping, but no registers
700721 index = index + 1
701- stack .append ([( '(' , - 1 )])
722+ stack .append ([OpenParen ( - 1 )])
702723
703724 elif pattern [index ] == '#' :
704725 # comment
0 commit comments