PREV UP next Regex

3.6.2: The Range Operator (-)

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:

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.