ITPEC FE Morning October 2018 Question 5

A regular expression [A-Z]+[0-9]* represents a set of character strings. Which of the following character strings belongs to this set? Here, the regular expression follows the rules below.

[A-Z] represents a single uppercase English character.

[0-9] represents a single decimal digit.

* indicates zero or more occurrences of the preceding element.

+ indicates one or more occurrences of the preceding element.

Explanation

ABCDEF — one or more uppercase letters followed by zero digits, so it fits [A-Z]+[0-9]*.

The pattern allows only two kinds of character, in a fixed order:

  • •[A-Z]+ — at least one uppercase letter, first
  • •[0-9]* — then any number of digits, including none

ABCDEF is six letters and no digits: [A-Z]+ takes all six and [0-9]* matches the empty rest.

Answer (d)

Why not others:

  • •(a) 456789 — begins with a digit, but [A-Z]+ requires at least one letter first
  • •(b) ABC+99 — contains the character +; in the pattern + is an operator meaning "one or more", not a character that can appear in the string
  • •(c) ABC99* — the same trap: the trailing * character matches neither [A-Z] nor [0-9]; the * in the pattern is the "zero or more" operator

Key rule: In a regular expression, + and * are repetition operators applied to the element before them; they never stand for the characters + and * themselves unless escaped.

Basic Theory · AI-written, may contain errors

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.