@@ -180,6 +180,35 @@ def process_ip(self, ip):
180
180
)
181
181
return trunc_ip
182
182
183
+ def process_regex_match (self , match ):
184
+ """
185
+ This function processes a single regex match.
186
+
187
+ It returns the anonymized match as string and can be called with re.sub.
188
+
189
+ :param match: re.Match
190
+ :return: str
191
+ """
192
+ ret = []
193
+ last_pos = 0
194
+
195
+ for i , g in enumerate (match .groups (), start = 1 ):
196
+ if not g :
197
+ continue
198
+ ip_str , ip = self .extract_ip (g )
199
+ replacement = (
200
+ self .process_ip (ip ) if ip
201
+ else self .replace or g
202
+ )
203
+ ret .extend ((
204
+ match .group (0 )[last_pos :match .start (i ) - match .start (0 )],
205
+ str (replacement ),
206
+ ))
207
+ last_pos = match .end (i ) - match .start (0 )
208
+
209
+ ret .append (match [0 ][last_pos :])
210
+ return "" .join (ret )
211
+
183
212
def process_line_regex (self , line ):
184
213
"""
185
214
This function processes a single line based on the provided regex.
@@ -189,23 +218,7 @@ def process_line_regex(self, line):
189
218
:param line: str
190
219
:return: str
191
220
"""
192
- match = re .match (self .regex , line )
193
- if not match :
194
- logger .debug ("Regex did not match!" )
195
- return line
196
- groups = match .groups ()
197
-
198
- for m in set (groups ):
199
- if not m :
200
- continue
201
- ip_str , ip = self .extract_ip (m )
202
- if ip :
203
- trunc_ip = self .process_ip (ip )
204
- line = line .replace (ip_str , str (trunc_ip ))
205
- elif self .replace :
206
- line = line .replace (m , self .replace )
207
-
208
- return line
221
+ return re .sub (self .regex , self .process_regex_match , line )
209
222
210
223
def process_line_column (self , line ):
211
224
"""
0 commit comments