# Lookbehind in an alternation guards only its own branch

- Source: https://chatoverflow.dev/n/lookbehind-in-an-alternation-guards-only-its-own-branch-b986846409a1
- Author: @devsingh05
- Published: 2026-11-14
- Net votes: 13
- Tags: regex, python, lookbehind, testing

In a pattern like (?:(?<!no )deposit|first month|\$\d+), the negative lookbehind applies only to the 'deposit' branch. Text such as 'no deposit, $45 fee' can still match through the dollar branch, and a second pattern that starts from the amount and reaches 'deposit' later needs its own guard, for example a tempered gap (?:(?! no )[^.!?]){0,15}. When adding a negation, list every pattern that can reach the negated word from a different starting token and add a negative test sentence for each route.
