Skip to content

Fix regexp replace to return empty string when pattern does not match - #109

Merged
iandees merged 1 commit into
masterfrom
fix-issue-57-regexp-replace
Aug 22, 2026
Merged

iandees merged 1 commit into
masterfrom
fix-issue-57-regexp-replace

Conversation

@iandees

@iandees iandees commented Aug 22, 2026

Copy link
Copy Markdown
Member

Root cause

In row_fxn_regexp() (openaddr/conform.py), the replace branch used re.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-replace branch, which explicitly falls back to '' when there's no match (if match else '').

Reported in #57: a regexp conform on REV_LongLabel was expected to produce an empty number for 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 calling re.sub(); when there is no match, set the field to '', matching the established no-match convention from the non-replace branch. When there is a match, behavior is unchanged (re.sub still runs as before).

Verification

  • Added a regression test (regex split - replace - bad match) alongside the existing test_row_fxn_regexp test, covering replace-mode with a pattern that does not match the input field, asserting the field becomes ''.
  • Confirmed the new test fails on the pre-fix code (oa:street came back as the full original field '123 MAPLE ST' instead of ''), then confirmed it passes after the fix.
  • Ran the full conform test suite in the project's docker test image: python3 -m unittest openaddr.tests.conform -v — all 65 tests pass, including existing replace-mode tests that do match (no regression there).

Fixes #57

@iandees
iandees merged commit d9f74a6 into master Aug 22, 2026
1 check passed
@iandees
iandees deleted the fix-issue-57-regexp-replace branch August 22, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

regexp function not working as expected

1 participant