Regular Expressions Fundamentals Quiz Quiz

Explore key concepts of regular expressions with this quiz designed to assess your understanding of basic syntax, pattern matching, quantifiers, character classes, and special characters. Strengthen your foundation in regex and recognize common patterns used in text processing and search.

  1. Identifying Wildcards in Regex

    Which character in regular expressions acts as a wildcard, matching any single character except a newline?

    1. .
    2. *
    3. ?
    4. #

    Explanation: The dot (.) serves as a wildcard in regular expressions, matching any single character except for newline characters. The asterisk (*) is a quantifier that matches zero or more occurrences of the previous element, not any character. The question mark (?) changes the previous token to be optional, matching zero or one occurrence. The hash (#) has no special meaning in most regex engines. Thus, only the dot correctly represents the wildcard function.

  2. Understanding Quantifiers in Regular Expressions

    In the regex pattern 'a{2,4}', what does this quantifier specify about the letter 'a'?

    1. Matches exactly two occurrences of 'a'
    2. Matches zero or more occurrences of 'a'
    3. Matches between two and four consecutive 'a's
    4. Matches up to two occurrences of 'a'

    Explanation: The '{2,4}' quantifier in regular expressions specifies that the preceding element, in this case 'a', must appear at least two times and at most four times consecutively. 'Matches exactly two occurrences of 'a'' would require '{2}'. 'Matches zero or more occurrences of 'a'' describes the '*' quantifier, and 'Matches up to two occurrences of 'a'' would be '{,2}', which is incorrect syntax. Therefore, 'Matches between two and four consecutive 'a's' is correct.

  3. Applying Character Classes in Regex

    Given the regex pattern '[A-Za-z]', what type of characters will this pattern match?

    1. Only uppercase letters
    2. Any single uppercase or lowercase English letter
    3. Digits and letters
    4. Whitespace characters

    Explanation: The character class '[A-Za-z]' matches any single uppercase (A-Z) or lowercase (a-z) English letter. 'Only uppercase letters' would be matched by '[A-Z]' alone. 'Digits and letters' would require including digits in the class, like '[A-Za-z0-9]'. 'Whitespace characters' are matched by 's', not by this character class. Therefore, the correct answer is any single uppercase or lowercase English letter.

  4. Anchors in Regular Expressions

    Which anchor in regular expressions matches the beginning of a line or string?

    1. ^
    2. $
    3. b
    4. &

    Explanation: The caret (^) symbol in regular expressions is used as an anchor to match the start of a line or string. The dollar sign ($) matches the end of a line or string. The 'b' is a word boundary anchor, not specifically related to the start. The ampersand (&) has no special regex meaning as an anchor. Thus, the caret is the correct choice for matching the beginning of a line.

  5. Special Meaning of the Question Mark in Regex

    How does the question mark (?) function when used after a character or group in a regular expression?

    1. It matches any character except a newline
    2. It denotes zero or one occurrence of the preceding element
    3. It matches the end of a string
    4. It matches one or more occurrences of the preceding element

    Explanation: The question mark (?) in regex acts as a quantifier that matches zero or one occurrence of the preceding character or group, making it optional. 'It matches any character except a newline' describes the '.' character, not '?'. 'It matches the end of a string' is the role of the '$' anchor. 'It matches one or more occurrences of the preceding element' is described by the '+' quantifier. Therefore, the question mark denotes zero or one occurrence.