Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

A programmer needs to replace every occurrence of 'cat' with 'dog' in a string s, but only if 'cat' is not preceded by 'big'. Which regex substitution would achieve this?

⚠ Common exam trap

The PCAP exam often tests the distinction between positive and negative lookbehinds, and the trap here is that candidates confuse `(?<=...)` (match if preceded by) with `(?<!...)` (match if NOT preceded by), leading them to choose Option C instead of D.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

re.sub(r'(?<!big)cat', 'dog', s)

Uses a negative lookbehind assertion `(?<!big)` to match 'cat' only when it is NOT preceded by 'big'. This ensures that 'bigcat' remains unchanged while standalone 'cat' is replaced with 'dog'. The `re.sub` function then substitutes all such matches in the string.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    re.sub(r'bigcat', 'dog', s)

    Why it's wrong here

    This pattern matches the six-character sequence 'bigcat' and replaces it with 'dog', so it never affects a bare 'cat' that appears elsewhere. For example, 'a cat and a bigcat' becomes 'a cat and a dog', leaving the ordinary 'cat' unreplaced. That violates the requirement to replace every occurrence of 'cat' except those in 'bigcat', and it also removes the 'big' prefix instead of preserving it.

  • ✗

    re.sub(r'cat', 'dog', s)

    Why it's wrong here

    Matching the literal three-letter pattern 'cat' makes the regex engine match the substring 'cat' inside 'bigcat' as well, because re.sub does not care about surrounding characters unless the pattern says so. Thus 'bigcat' becomes 'bigdog' after substitution, which is precisely the case that should remain untouched. A correct solution must use a negative lookbehind to stop matches when the preceding text is 'big'.

  • ✗

    re.sub(r'(?<=big)cat', 'dog', s)

    Why it's wrong here

    The positive lookbehind (?<=big) restricts matches to 'cat' only when the preceding characters are exactly 'big'. Consequently, this regex replaces the protected words 'bigcat' with 'bigdog' and leaves all other standalone 'cat' occurrences unchanged, which is the opposite of what the programmer needs. The desired behavior requires a negative lookbehind to exclude that context rather than require it.

  • ✓

    re.sub(r'(?<!big)cat', 'dog', s)

    Why this is correct

    The negative lookbehind (?<!big) is a zero-width assertion that succeeds only when the characters immediately before the current position are not 'big'; if they are, the match attempt at that location fails. As a result, every 'cat' that is not part of 'bigcat' is replaced with 'dog', while 'bigcat' stays exactly as is. This correctly replaces all occurrences of 'cat' except those preceded by 'big'.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.