Skip to content

fix(cleaners): anchor the whole pattern in clean_prefix and clean_postfix - #4861

Open
sarankumar1325 wants to merge 1 commit into
Unstructured-IO:mainfrom
sarankumar1325:fix/clean-prefix-postfix-alternation
Open

sarankumar1325 wants to merge 1 commit into
Unstructured-IO:mainfrom
sarankumar1325:fix/clean-prefix-postfix-alternation

Conversation

@sarankumar1325

@sarankumar1325 sarankumar1325 commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

clean_prefix and clean_postfix interpolate the caller's pattern straight into ^{pattern} and {pattern}$. For a pattern with an alternation, the anchor only binds one branch (^Hi|World is "Hi at the start, or World anywhere"):

>>> clean_prefix("Hello World", "Hi|World")
'Hello '          # World removed from the middle
>>> clean_postfix("Hi there", "Hi|World")
'there'           # Hi removed from the start

The docstrings say the pattern "can be a simple string or a regex pattern", and the same applies through clean(prefix=..., postfix=...). The existing tests only use a parenthesised alternation ((END|STOP)), which is why this went unnoticed.

The fix wraps the pattern in a non-capturing group (^(?:...) / (?:...)$), so the anchor applies to every branch. Patterns that already parenthesise their alternation behave exactly as before.

Validation

  • Added test_clean_prefix_anchors_every_alternative and test_clean_postfix_anchors_every_alternative; 5 of their cases fail without the fix.
  • pytest test_unstructured/cleaners/test_core.py: 172 passed.
  • ruff check and ruff format --check clean on the two touched files.

Release note

clean_prefix / clean_postfix no longer remove a match from the wrong end of the text when the pattern contains an unparenthesised |.

AI-assisted change. I reviewed the diff and ran the tests locally. I can't apply labels; this is a bug fix.

View guided diff

…tfix

The pattern was interpolated into ^{pattern} and {pattern}$, so an
alternation like "Hi|World" anchored only its first (prefix) or last
(postfix) branch. clean_prefix("Hello World", "Hi|World") removed World
from the middle of the text, and clean_postfix("Hi there", "Hi|World")
removed Hi from the start.

Wrap the pattern in a non-capturing group so the anchor applies to every
branch.

Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-26T15:57:16.852880Z
We couldn’t complete this review. Comment @codex review to try again.
f159ee8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

This branch has not been deployed

No deployments
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.

1 participant