Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current implementation uses
re.match
and hence only replaces groups in the first match of the regexp. This PR changes that behavior to instead usere.finditer
to replace all groups in all matches.This actually makes it possible/easier to match all possible IP addresses (without depending on their surrounding context) by a user-supplied regexp. See the added testcase for an example regex that matches much more than IP addresses, but in particular also IP addresses (which is unproblematic if
replace == None
since the real verification takes place using theipaddress
module).Also, the access log line in my newly added test case, with the
[ip:1.2.3.4]
at the end, actually appeared in an access log, with different IP addresses in the beginning and at the end!This PR also closes #61 by using match group's indices for the replacement instead of
str.replace
N.B.: I haven't verified that all newly used methods and features (in particular
re.Match.start()
,re.Match.end()
andenumerate(start=1)
) are available in all supported Python versions.N.B.: I haven't evaluated the performance impact of this change.
(Rant: The choice to replace only groups instead of using look-aheads and look-behinds in the pattern made this change quite a bit more cumbersome, but I'm 10 days too late to change that I guess)