# Numeric Ranges

Regular expressions may have extensive functionality, but they are designed to match text, so matching a numeric range requires some extra work. Since the need to match numeric ranges is sometimes necessary, RegExPlus has built-in support for handling them.

#### Range basics

When specifying a range, you can allow leading zeros,

Syntax: `(?Z[start..end])` - (the Z is a mnemonic for zeros)

Syntax: `(?NZ[start..end])`

If you allow leading zeros, you can specify how many digits the match will be (that is, the match's width).

A range can be specified as either `[start..end]` or `[end..start]`.

Negative numbers are supported, as well as ranges specified in a different base.

To match a numeric range that allows leading zeros, use the syntax:

`(?Z[start..end])` - the Z is a mnemonic for zeros

Start is the start of the numeric range, and end is the end.

##### Match's width

When allowing leading zeros, there is a way to specify the number of digits in a valid match.

For example, the range `0..59` would match a number between 0 and 59 with one or two digits (that is, a leading zero is optional for numbers less than 10). Whereas, the range `00..59` would match a number between 0 and 59, but must have two digits (that is, a leading zero for numbers less than 10 is required).

In general, the number of digits for a valid match must be between the number of digits in start and in end.

##### Match's width (mixing positive and negative bounds)

If one of the bounds is positive and one of the bounds is negative, the match's width is as follows.

For the positive numbers

the number of digits in a match is between 1 and the number of digits in the positive bound.

For the negative numbers,

the number of digits in a match is between 1 and the number of digits in the negative bound.

For example, the range `-12..123` matches a number between -12 and 123. For the negative numbers, the number of digits must be between 1 and 2. For the positive numbers, the number of digits must be between 1 and 3.

Syntax: `(?Z[start..end])` - the Z is a mnemonic for zeros

`0000..23` matches a number between 0 and 23, and must have between 2 and 4 digits.

`001..0024` matches a number between 1 and 24, and must have either 3 or 4 digits.

`-12..123` matches a number between -12 and 123. For the negative numbers, the number of digits must be either 1 or 2. For the positive numbers, the number of digits must be between 1 and 3.

The other way to match a numeric range has no leading zeros as part of the match. The syntax to do this is `(?NZ[start..end])`.

#### Examples of common ranges

The linked tutorial on numeric ranges has several common ranges. The below examples show how to match them using the numeric range syntax.

Below are some examples of common ranges and how to match them using the numeric range syntax.

Type 1: `X or X..Y` - matches a number between X and Y, leading zeros are optional.

`0 or 000..255`:  `(?Z[0..255])`

`0 or 000..127`:  `(?Z[0..127])`

`0 or 000..999`:  `(?Z[0..999])`

`1 or 001..999`:  `(?Z[1..999])`

`0 or 00..59`:  `(?Z[0..59])`

`0 or 000..366`:  `(?Z[0..366])`

Type 2: `00X..Y` - matches a number between X and Y, requiring the number to be a specific width.

`000..255`:  `(?Z[000..255])`

`000..999`:  `(?Z[000..999])`

`001..999`:  `(?Z[001..999])`

Type 3: `X..Y` - matches a number between X and Y, leading zeros are not allowed.

`0..999`:  `(?NZ[0..999])`

`1..999`:  `(?NZ[1..999])`

#### RegEx for range

Included in the Pattern class are static functions able to output a regular expression for the inputted range. The returned value is wrapped in a non-capture group to allow easy integration.

• static String range(int start, int end, String mode)
• static String range(String start, String end, String mode)

RegExPlus uses these functions, internally, to provide its support for numeric ranges.

#### Negative values in range

You can specify a negative number for the start, the end, or both - the syntax doesn't change.

#### Range in a different base

For most needs, the range you specify will be in base 10. For example, to match an IPv4 address in dot-decimal notation, you would use the range `0..255` for each of the four parts.

For most needs, the range you specify will be in base 10. For example, to match an IPv4 address in dot-decimal notation, you would use the range `0..255` for each of the four parts.

However, let's say you wanted to match an IPv4 address in dotted hexadecimal notation. For each part, you would match the (hexadecimal) numbers in the range `0..FF`. The syntax for the regex to match this range is `(?Z16[0..FF])` to allow leading zeros and `(?NZ16[0..FF])` to match a number with no leading zeros.

##### Optional L/U

When the base is above 10, digits greater than 10 are represented by letters. For example, For base 16, the digits 0-9 are used, as well as A-F.

By default, both upper-case and lower-case are allowed in a match. However, it may be desired to limit the regex to only accept numbers that use lower-case digits, or only that use upper-case ones.

To allow only lower-case digits, specify an "L" (case-sensitive) after the base. For example, the regex `(?NZ16L[0..FF])` would match `ff`, but not `FF`, whereas the regex `(?NZ16[0..FF])` would match both. To allow only upper-case digits, specify a "U" (case-sensitive).

Note 1: this setting only affects matches - when specifying the range in the pattern, both upper and lower-case digits are allowed, regardless of this setting.

Note 2: although the "L/U" setting has no effect for bases less than 11, for consistency, it can be specified. For example, `(?NZ10L[start..end])`,