Alternation and Pipe (|) Fundamentals in Regular Expressions Quiz

This quiz evaluates your understanding of alternation and the pipe symbol in regular expressions, focusing on their syntax, behavior, and typical use cases. Strengthen your grasp of regex patterns used for matching multiple possibilities and crafting efficient search expressions.

  1. Identifying Alternation Syntax

    Which regular expression pattern uses the pipe (|) operator to match either 'cat' or 'dog' in a string?

    1. cat|dog
    2. cat/dog
    3. [cat|dog]
    4. cat||dog

    Explanation: The pipe symbol (|) in a regular expression is used as an alternation operator to match either the pattern on its left or its right, so 'cat|dog' matches either 'cat' or 'dog'. 'cat/dog' uses a slash, which is not alternation in regex. '[cat|dog]' is a character class and matches any of the listed characters, not the words. 'cat||dog' is not a valid syntax in regular expressions.

  2. Alternation Order Importance

    If you use the regular expression 'hand|handy', what will the pattern match in the string 'handywork'?

    1. hand
    2. handy
    3. handywork
    4. Neither

    Explanation: In regex engines, alternation is evaluated from left to right and stops at the first successful match. Since 'hand' matches before 'handy' in 'handywork', the match will be 'hand'. 'handy' is correct only if it appeared first in the alternation order, and 'handywork' would only match if the entire string matched the pattern, which it does not. 'Neither' is incorrect as 'hand' is present.

  3. Alternation with Groups

    Given the pattern 'gr(a|e)y', which of the following words would be matched?

    1. grey
    2. gryi
    3. gry
    4. green

    Explanation: The pattern uses parentheses to group 'a' and 'e' as possible options, so 'gr(a|e)y' matches either 'gray' or 'grey'. 'grey' fits this pattern, while 'gryi' and 'gry' do not have 'a' or 'e' in the required position. 'green' does not fit because the pattern expects only one character between 'gr' and 'y'.

  4. Alternation vs. Character Classes

    What is the difference between the regular expressions 'l(i|e)ad' and 'l[ie]ad'?

    1. The former matches 'liad' or 'lead'; the latter also allows 'lad' and 'l[iad]'.
    2. They function identically and match the same words.
    3. The former matches only 'liad', while the latter matches 'lead'.
    4. The former matches full words, the latter matches single characters.

    Explanation: 'l(i|e)ad' matches only 'liad' or 'lead' due to explicit alternation between 'i' or 'e'. 'l[ie]ad' matches any 'l' followed by either 'i' or 'e', then 'ad', so it matches 'liad' and 'lead' but also words like 'lad' if 'd' is considered within the character set and 'l[iad]' as a literal string, which is incorrect. Saying they are identical is wrong, and the other options mischaracterize their function.

  5. Alternation and Quantifiers

    In the pattern '(foo|bar)+', which of the following strings will match?

    1. foofoo
    2. foobarbarz
    3. barfoo
    4. baz

    Explanation: The pattern '(foo|bar)+' matches one or more consecutive occurrences of either 'foo' or 'bar'. 'foofoo' matches because it is two 'foo' substrings. 'barfoo' also matches as it is 'bar' followed by 'foo'. 'foobarbarz' does not fully match as the ending 'z' is not covered by the pattern. 'baz' does not match since 'baz' is not 'foo' or 'bar'.