ACE-P-ALE1-04 practice question 31 of 35

To negate a set in a regular expression pattern, which character is used?

Full set: 100 questions
Answer options
A: !
B: ^ Correct answer
C: ?
D: -
Correct Answer:
B. ^
Explanation

In standard regular expression syntax, the caret or circumflex (^) character is used to negate a character set. When ^ is the very first character immediately following the opening square bracket [, it inverts the meaning of the set. Instead of matching any character within the set, the pattern will match any single character not within the set. For example, the regular expression [^aeiou] will match any character that is not a lowercase vowel.

Why Incorrect

A. ! is used for negative lookaheads (?!...) in some regex flavors but does not negate a character set.
C. ? is a quantifier that makes the preceding element optional, matching it zero or one time.
D. - is used inside a character set [] to specify a range of characters, such as [a-z]. ---

References

1. Python 3 Documentation, re — Regular expression operations:

Reference: In the "Regular Expression Syntax" section, under the description for [...], it states: "If the first character of the set is '^', all the characters that are not in the set will be matched." This is a primary source as Python scripting is a key component of the Arista Linux Environment.

Source: Python Software Foundation, re — Regular expression operations, Section 7.2.1. Available at: https://docs.python.org/3/library/re.html#regular-expression-syntax

2. Arista EOS 4.28.1F Configuration Guide:

Reference: In the section on CLI fundamentals, the guide explains how to filter command output using regular expressions with grep. It describes the use of character classes [...] and explicitly mentions negation: "To match any character except those in the list, the first character in the list must be a caret (^)."

Source: Arista Networks, EOS 4.28.1F Configuration Guide, Chapter 4: "CLI Fundamentals", Section: "Filtering show Command Output", Page 78.

3. MIT OpenCourseWare, "The Missing Semester of Your CS Education":

Reference: Lecture 2, "Shell Tools and Scripting," covers regular expressions used with tools like grep. The lecture notes explicitly demonstrate that [^...] is used to match any character that is not in the set.

Source: MIT, The Missing Semester of Your CS Education (2020), Lecture 2 Notes. Available at: https://missing.csail.mit.edu/2020/shell-tools/

Scroll to Top