Regex Lookaheads and Lookbehinds Quiz Quiz

Challenge your understanding of lookaheads and lookbehinds in regular expressions with five focused questions. This quiz helps clarify concepts behind positive and negative lookarounds, their syntax, and practical regex scenarios for pattern matching.

  1. Identifying Positive Lookahead Syntax

    Which of the following syntaxes correctly represents a positive lookahead in a regular expression?

    1. (?=...)
    2. (?!...)
    3. (?<=...)
    4. (?<!...)

    Explanation: The correct syntax for a positive lookahead is (?=...), used to check if a given pattern follows without including it in the match. (?!...) is a negative lookahead, which checks that a pattern does not follow. (?<=...) and (?<!...) are lookbehind expressions, with the former being positive and the latter negative. Remember, lookaheads look forward in the pattern, while lookbehinds look backward.

  2. Distinguishing Lookbehind Behavior

    In the string 'catdog', which regex pattern will match 'dog' only if it is immediately preceded by 'cat'?

    1. cat(?=dog)
    2. (?<=cat)dog
    3. dog(?<=cat)
    4. (?=!cat)dog

    Explanation: The pattern (?<=cat)dog uses a positive lookbehind to match 'dog' only when it is directly preceded by 'cat'. cat(?=dog) would match 'cat' when followed by 'dog', not 'dog' itself. dog(?<=cat) is incorrect syntax, as the lookbehind must appear before what it is influencing. (?!cat)dog is a negative lookahead, not a valid way to check for 'cat' before 'dog'.

  3. Choosing Negative Lookahead for Exclusions

    Which regex pattern would match the word 'apple' only if it is not immediately followed by the word 'pie'?

    1. apple(?! pie)
    2. apple(?= pie)
    3. (?<=apple)pie
    4. (?<!apple)pie

    Explanation: apple(?! pie) uses a negative lookahead to ensure 'apple' is not directly followed by ' pie'. apple(?= pie) uses a positive lookahead and would match 'apple' only when followed by ' pie'. (?<=apple)pie is a positive lookbehind used to match 'pie' after 'apple', and (?<!apple)pie is a negative lookbehind matching 'pie' not preceded by 'apple'. Only the negative lookahead fits the exclusion condition provided.

  4. Application of Nested Lookarounds

    In which scenario is using both a lookahead and a lookbehind useful within a single regex pattern?

    1. When extracting text only if it appears between two specific words
    2. When matching a word at the end of a line regardless of context
    3. When globally replacing every occurrence of a pattern
    4. When counting the total number of matching lines in a document

    Explanation: Lookaheads and lookbehinds can be combined to extract text that occurs between two specific patterns, ensuring both the preceding and following context. Matching a word at the end of a line usually requires line anchors, not lookarounds. Global replacements and counting lines are general regex tasks and do not require both lookahead and lookbehind together. The ability to define both left and right boundaries is a unique advantage of nesting these constructs.

  5. Understanding Lookbehind Limitations

    Why might variable-length lookbehinds fail or be unsupported in some regular expression engines?

    1. They require more computational resources and can cause ambiguity
    2. They match unlimited times by default
    3. They always consume the matched characters
    4. They only work in single-line mode

    Explanation: Some regex engines do not support variable-length lookbehinds because they can be computationally expensive and lead to ambiguous matching scenarios. Unlike lookaheads, lookbehinds require the length of the pattern to be determinable for efficient backward scanning. The other choices are not accurate; lookbehinds do not match unlimited times nor do they inherently consume matched characters, and they are not restricted to single-line mode.