Fix regexp replace to return empty string when pattern does not match - #109
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Root cause
In
row_fxn_regexp()(openaddr/conform.py), thereplacebranch usedre.sub()directly on the field value.re.sub()returns the original, unmodified string when the pattern never matches anywhere in it, so a row whose field didn't match the configured pattern silently kept the full original field value instead of becoming empty.This is inconsistent with the non-
replacebranch, which explicitly falls back to''when there's no match (if match else '').Reported in #57: a
regexpconform onREV_LongLabelwas expected to produce an emptynumberfor rows where the field held only a zip/city/state/country (no house number to match), but it returned the full field value instead.Fix
Check
pattern.search(value)before callingre.sub(); when there is no match, set the field to'', matching the established no-match convention from the non-replacebranch. When there is a match, behavior is unchanged (re.substill runs as before).Verification
regex split - replace - bad match) alongside the existingtest_row_fxn_regexptest, covering replace-mode with a pattern that does not match the input field, asserting the field becomes''.oa:streetcame back as the full original field'123 MAPLE ST'instead of''), then confirmed it passes after the fix.python3 -m unittest openaddr.tests.conform -v— all 65 tests pass, including existing replace-mode tests that do match (no regression there).Fixes #57