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.
Which metacharacter combination would match any single digit or lowercase letter in the string 't3st'?
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.
In the pattern 'a.*?b' applied to the string 'a123b456b', what would the match capture?
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.
Which metacharacters should be used to ensure a regex pattern matches only at the beginning of a line in a multiline text?
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.
Given the pattern 'a.b' and the string 'anb', how would the default dot (.) metacharacter behave?
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.
Which regex pattern correctly matches the dollar sign character '$' in a string such as 'Price: $10'?
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.