1

So far I have:

^[1-6],[1-6]$

The above will permit a comma delimited expression of two numbers where each number must be between 1 and 6.

Is it possible to include a comparison in the expression which would mean that the first number must be exactly 1 less than the second number and that the first number and second numbers must be different?

SPlatten
  • 5,334
  • 11
  • 57
  • 128

1 Answers1

0

The top comments are correct: there is no way other than "brute force" or "spelling out" alternatives as there is no way to do math inside a regular expression.

Use any version, with capturing or non-capturing groups:

^(?:1,2|2,3|3,4|4,5|5,6)$
^(1,2|2,3|3,4|4,5|5,6)$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    1,2                      '1,2'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    2,3                      '2,3'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    3,4                      '3,4'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    4,5                      '4,5'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    5,6                      '5,6'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37