2025, Oct 25 15:00
Match ABC Everywhere Except the Fourth Position: Using a Negative Lookbehind Anchored to Start (Python Regex)
Learn how to match ABC everywhere except at the 4th character using a position-aware regex. Negative lookbehind anchored to start with Python examples.
Excluding a specific position from a regex match can look tricky until you realize that lookarounds let you express “match this, but not if it sits exactly here.” The task: find every occurrence of ABC in a string except when it starts at the fourth character.
What we want to avoid
ABC should match in most positions, just not when its starting index is the fourth character of the string. In other words, anything like FGHABCDE must be excluded, while ABCDEFGH, HABCDEFG, GHABCDEF, and EFGHABCD should match.
Naive approach: matches everything
A plain search for ABC will pick up every occurrence, including the unwanted one at the fourth position. That demonstrates the problem neatly:
import re
samples = [
    "ABCDEFGH",
    "HABCDEFG",
    "GHABCDEF",
    "FGHABCDE",
    "EFGHABCD"
]
plain = re.compile(r"ABC")
for text in samples:
    print(text, bool(plain.search(text)))
As expected, this approach offers no control over where the match is allowed to occur.
Why this happens
Regex engines will happily match a literal pattern wherever it appears. To exclude a specific index, the pattern needs awareness of its surrounding context. This is where a negative lookbehind helps: it lets you assert that a certain condition is not true immediately before the current position, without consuming any characters.
The fix: a position-aware negative lookbehind
The pattern (?<!^.{3})ABC precisely encodes the rule “match ABC unless it’s located right after exactly three characters from the start of the string.” The ^.{3} part anchors at the beginning of the string and matches three characters; the negative lookbehind (?<!...) ensures that this exact context is not present immediately before ABC. The result is that the fourth-position occurrence is excluded, while all other positions are allowed.
import re
items = [
    "ABCDEFGH",
    "HABCDEFG",
    "GHABCDEF",
    "FGHABCDE",
    "EFGHABCD"
]
rx = re.compile(r"(?<!^.{3})ABC")
for row in items:
    print(row, bool(re.search(rx, row)))
This gives exactly the desired behavior: everything matches except the case where ABC begins at the fourth character.
Why it matters
Sometimes you need to keep all valid matches while excluding a razor-specific position or context. Negative lookbehind with an anchored subpattern provides a compact and precise way to express that rule directly in regex. This is especially useful when the goal is to “do everything in regex” without additional filtering logic elsewhere.
Takeaways
If you need to allow a pattern everywhere except at a certain start index, anchor a negative lookbehind to the beginning of the string and count the characters you want to forbid. For the “everything but the fourth position” case, (?<!^.{3})ABC is a concise and reliable choice. Keep this technique in your toolbox whenever positional exclusions appear in parsing, validation, or text processing tasks.
The article is based on a question from StackOverflow by Branden Keck and an answer by ruohola.