LFCS practice question 7 of 35

What does the ? symbol within regular expressions represent?

Full set: 260 questions
Answer options
A: Match the preceding qualifier one or more times.
B: Match the preceding qualifier zero or more times.
C: Match the preceding qualifier zero or one times. Correct answer
D: Match a literal ? character.
Correct Answer:
C. Match the preceding qualifier zero or one times.
Explanation

In regular expressions, the question mark (?) is a quantifier metacharacter. It specifies that the preceding character, group, or character class is optional, meaning it can be matched either zero times or exactly one time. For example, the regular expression colou?r would match both "color" (zero 'u's) and "colour" (one 'u').

Why Incorrect

A. Matching one or more times is represented by the plus sign (+) quantifier.
B. Matching zero or more times is represented by the asterisk () quantifier.
D. To match a literal question mark character, it must be escaped with a backslash, as in \?.

References

1. Linux man-pages project. (2023). regex(7) manual page. Section: Regular Expression Metacharacters. (This page details POSIX regular expression syntax, defining ? as matching the preceding item zero or one time).

2. The Open Group Base Specifications Issue 7, 2018 edition. (2018). Chapter 9: Regular Expressions, Section 9.3.5: ERE Quantifiers. IEEE Std 1003.1-2017. Retrieved from https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1chap09.html#tag090305. (This POSIX standard defines ? as matching the preceding element zero or one time).

Scroll to Top