@@ -282,7 +282,7 @@ def reconfig(self, file, pattern, replacement):
282
282
# Not found; append (silently)
283
283
self .write_text_file (file , replacement , append = True )
284
284
285
- def pattern_search (self , location , pattern , multi_line = False ):
285
+ def pattern_search (self , location , pattern , multi_line = False , return_match = False ):
286
286
"""
287
287
Similar to grep, but uses pure python
288
288
multi_line will search the entire file as a large text glob,
@@ -296,13 +296,17 @@ def pattern_search(self, location, pattern, multi_line=False):
296
296
if self .exists (location ) and not self .isdir (location ):
297
297
if multi_line :
298
298
with open (location , "r+" , encoding = "utf-8" ) as file :
299
- if re .search (pattern , file .read (), flags = re .DOTALL ):
299
+ match = re .search (pattern , file .read (), flags = re .DOTALL )
300
+ if match :
300
301
found = True
301
302
else :
302
303
for line in fileinput .FileInput (location ):
303
- if re .search (pattern , line ):
304
+ match = re .search (pattern , line )
305
+ if match :
304
306
found = True
305
-
307
+ break
308
+ if return_match :
309
+ return match
306
310
return found
307
311
308
312
def pattern_replace (self , location , pattern , replace = "" , multi_line = False ):
0 commit comments