Name

? (Question Mark) — Matches zero or one

Synopsis

The question mark (?) is very similar to the asterisk (*), except that it matches at most one occurrence of the preceding element. For example, the following returns only the first fruit:

SELECT REGEXP_SUBSTR('apple apple orange wheat',
       '((apple|orange|pear)[[:space:]]*)?')
FROM dual;

apple

Like the *, the ? can surprise you by matching where you don’t expect. In this case, if the string doesn’t begin with a fruit name, the ? will match on the empty string. See * (Asterisk) for an example of this kind of behavior.

Get Oracle Regular Expressions Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.