Nico
Nico

Reputation: 1838

iOS - Regular expression for 4 digit int that can't be sequential or repeated digits?

I am trying to get a Regex that checks to make sure that a supplied int is 4 digits and it is not sequential nor contains all repeating digits (applied for 3 digits as well) whether in ascending or descending order. I don't really care if the regex returns a match for the non-allowed numbers, or returns a match of the original number if it is allowed.

So for example all of these numbers are what I would need to not pass validation with the regex:

While numbers like these would pass:

In other words,

Thanks.

Upvotes: 0

Views: 2129

Answers (1)

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

^(?!.*?(?:0(?:12|98)|123|234|3(?:45|21)|4(?:56|32)|5(?:67|43)|6(?:78|54)|7(?:89|65)|876|987))(?!.*?(.)\1{2})[0-9]{4}

Regular expression visualization

** To see the image better, simply right click the image and select view in new window

This regular expression will do the following:

  • Require the string to be 4 numbers long

  • Will not allow the same three consecutive digits. E.g. 111 is not ok.

  • Will not allow continuous ascending or descending of three or more digits. E.g.678 or 321 is not ok.

Example

Live Demo

https://regex101.com/r/qS3bO8/2

Sample text

So for example all of these numbers are what I would need to not pass validation with the regex:

1234
6543
4567
3333
3331
1333
1239
3219
1789
2543
While numbers like these would pass:

0443
6690
0420
6798

Sample Matches

MATCH 1
0.  [186-190]   `0443`

MATCH 2
0.  [191-195]   `6690`

MATCH 3
0.  [196-200]   `0420`

MATCH 4
0.  [201-205]   `6798`

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      0                        '0'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        12                       '12'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        98                       '98'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      123                      '123'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      234                      '234'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      3                        '3'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        45                       '45'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        21                       '21'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      4                        '4'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        56                       '56'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        32                       '32'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      5                        '5'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        67                       '67'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        43                       '43'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      6                        '6'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        78                       '78'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        54                       '54'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      7                        '7'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        89                       '89'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        65                       '65'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      876                      '876'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      987                      '987'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      .                        any character except \n
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    \1{2}                    what was matched by capture \1 (2 times)
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  [0-9]{4}                 any character of: '0' to '9' (4 times)
----------------------------------------------------------------------

Upvotes: 3

Related Questions