Regex Quantifiers: Unlocking *, +, ?, and {} Quiz

Deepen your understanding of regex quantifiers such as *, +, ?, and {}. This quiz is designed to help you differentiate quantifier functions, interpret matching behaviors in patterns, and avoid common misunderstandings in regular expressions.

  1. The Asterisk Quantifier

    In the regex pattern 'ba*', what types of strings will it successfully match?

    1. Any string that starts with 'b' and has zero or more 'a's afterward, such as 'b', 'ba', or 'baaa'.
    2. Only strings containing at least one 'a' after 'b', like 'ba' or 'baaa'.
    3. Strings containing exactly one 'a' after 'b', such as 'ba'.
    4. Strings that do not contain the letter 'b' at all.

    Explanation: The '*' quantifier in regex matches zero or more occurrences of the character before it, so 'ba*' matches 'b' followed by any number of 'a's, including none at all. Option 2 is incorrect because it wrongly excludes strings where 'a' appears zero times. Option 3 is too restrictive, as it limits to exactly one 'a'. Option 4 is irrelevant since the pattern explicitly begins with 'b'.

  2. The Plus Quantifier

    If you want to ensure a pattern matches one or more consecutive digits (like '3', '78', or '1234') but never an empty string, which quantifier should you use in the regex 'd+'?

    1. The plus quantifier '+'.
    2. The asterisk quantifier '*'.
    3. The question mark quantifier '?'.
    4. The curly braces quantifier '{}', written as 'd{0,}'.

    Explanation: The plus quantifier '+' matches one or more occurrences, so 'd+' will match digits sequences like '7' or '456' but not the empty string. The asterisk '*' allows zero matches, so 'd*' could match an empty string. The question mark '?' would match only zero or one digit, not consecutive digits. The curly braces 'd{0,}' also allow for zero digits, effectively being equivalent to '*', thereby matching the empty string as well.

  3. The Question Mark Quantifier

    Given the regex 'colou?r', which spelling variants would it match in a text?

    1. Both 'color' and 'colour'.
    2. Only 'colour', never 'color'.
    3. All words ending with 'ur', such as 'favour' and 'honour'.
    4. Only the word 'coloru'.

    Explanation: The question mark quantifier '?' makes the previous character optional by allowing zero or one instance. In 'colou?r', the 'u' is optional, so it matches both 'color' and 'colour'. Option 2 is incorrect because it fails to recognize 'color' as a match. Option 3 misinterprets the pattern, extending beyond the intended structure. Option 4 is unrelated, as the pattern never matches 'coloru'.

  4. Curly Braces Quantifier in Patterns

    Which regex pattern matches a string containing exactly three consecutive 'x' characters?

    1. 'x{3}'
    2. 'x{2,}'
    3. 'x+'
    4. 'x?'

    Explanation: The curly braces quantifier with a single value, as in 'x{3}', specifies exactly three consecutive occurrences of 'x'. 'x{2,}' demands two or more, not exactly three. 'x+' matches one or more, including numbers other than three. 'x?' matches zero or one occurrence only, which does not satisfy the requirement for exactly three.

  5. Interpreting Quantifiers in Email-like Usernames

    Which regex pattern would be most appropriate to match usernames with at least two lowercase letters but allow more, such as 'ab', 'abc', or 'alphabet'?

    1. '[a-z]{2,}'
    2. '[a-z]+'
    3. '[a-z]{2}'
    4. '[a-z]*'

    Explanation: '[a-z]{2,}' means at least two lowercase letters, matching 'ab', 'abc', and longer usernames. '[a-z]+' matches one or more, failing to enforce the minimum of two. '[a-z]{2}' only matches exactly two letters, so longer names like 'abc' are not matched as entire usernames. '[a-z]*' would match usernames with zero or more letters, so even an empty string would match, which is not suitable.