-
)
Regex recognizes range expressions inside a list. They represent
those characters
that fall between two elements in the current collating sequence. You
form a range expression by putting a range operator between two
characters.[1] -
represents the range operator. For example,
a-f
within a list represents all the characters from a
through f
inclusively.
If the syntax bit RE_NO_EMPTY_RANGES
is set, then if the range's
ending point collates less than its starting point, the range (and the
regular expression containing it) is invalid. For example, the regular
expression [z-a]
would be invalid. If this bit isn't set, then
Regex considers such a range to be empty.
Since -
represents the range operator, if you want to make a
-
character itself
a list item, you must do one of the following:
-
either first or last in the list.
-
and whose ending point collates equal or higher. Unless a
range is the first item in a list, a -
can't be its starting
point, but can be its ending point. That is because Regex
considers -
to be the range operator unless it is preceded by
another -
. For example, in the ascii encoding, )
,
*
, +
, ,
, -
, .
, and /
are
contiguous characters in the collating sequence. You might think that
[)-+--/]
has two ranges: )-+
and --/
. Rather, it
has the ranges )-+
and +--
, plus the character /
, so
it matches, e.g., ,
, not .
.
-
first in the list.
For example, [-a-z]
matches a lowercase letter or a hyphen (in
English, in ascii).
[1] You can't use a character class for the starting or ending point of a range, since a character class is not a single character.