Regex Metacharacters: Comprehensive Application Quiz Quiz

Challenge your understanding of regex metacharacters with this quiz focused on practical applications and subtle distinctions in pattern matching. Gain insights into using essential regex symbols effectively for text processing and validation.

  1. Character Classes in Regex

    Which metacharacter combination would match any single digit or lowercase letter in the string 't3st'?

    1. [a-z0-9]
    2. (a-z0-9)
    3. {a-z0-9}
    4. <a-z0-9>

    Explanation: The correct answer is [a-z0-9], which defines a character class matching any single lowercase letter or digit. (a-z0-9) is interpreted as a capturing group, not a character class, so it does not select individual characters. {a-z0-9} and <a-z0-9> are not valid regex syntax for character classes and would result in errors. Only square brackets may be used to specify a set of characters to match.

  2. Quantifiers: Greedy vs Lazy

    In the pattern 'a.*?b' applied to the string 'a123b456b', what would the match capture?

    1. a123b
    2. a123b456b
    3. a123
    4. a456b

    Explanation: The answer is a123b because the '.*?' is a lazy quantifier, meaning it matches as few characters as possible until reaching the next 'b'. Using a greedy quantifier '.*' would match up to the last 'b', resulting in 'a123b456b', which is incorrect here. 'a123' stops before 'b' and so is not correct, and 'a456b' ignores the first occurrence and is not what the pattern would match in left-to-right order.

  3. Anchors in Regex Patterns

    Which metacharacters should be used to ensure a regex pattern matches only at the beginning of a line in a multiline text?

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

    Explanation: ^ is the correct metacharacter for anchoring a pattern to the start of a line, especially in multiline mode. $ is used for anchoring at the end of a line, not the beginning. b represents a word boundary, which is unrelated to the start of a line. The ( character is used for grouping, not for position anchoring. Thus, ^ is the only accurate answer for this function.

  4. Dot Metacharacter and Newlines

    Given the pattern 'a.b' and the string 'anb', how would the default dot (.) metacharacter behave?

    1. It does not match because dot does not include newlines
    2. It matches across the newline character
    3. It throws a syntax error
    4. It matches any character including digits but not spaces

    Explanation: By default, the dot metacharacter (.) matches any character except newline characters like 'n'. Therefore, 'a.b' does not match 'anb'. It will not match across the newline unless a special modifier is used. It does not throw a syntax error, and the dot can match spaces just as it can match digits or letters, except for newlines. The correct behavior is that the pattern does not match.

  5. Escaping Special Characters

    Which regex pattern correctly matches the dollar sign character '$' in a string such as 'Price: $10'?

    1. $
    2. [$]
    3. $
    4. dollar

    Explanation: The dollar sign is a metacharacter in regex, used to anchor the end of a line. To match a literal '$', it must be escaped as '$'. '[$]' is incorrect because it creates a character class and unnecessarily escapes the closing bracket. '$' alone would anchor the end of the line rather than match the symbol. 'dollar' is not part of regex syntax and will look for the word 'dollar' instead. Thus, '$' is the correct answer.