iOS - 4位int的正则表达式,不能是连续或重复的数字?

时间:2016-06-27 04:08:06

标签: ios objective-c regex swift nsregularexpression

我试图得到一个正则表达式,它检查以确保提供的int是4位数,并且它不是顺序的,也不包含所有重复数字(也适用于3位数),无论是按升序还是降序。我真的不在乎正则表达式是否返回非允许数字的匹配项,或者如果允许则返回原始数字的匹配项。

因此,例如,所有这些数字都是我不需要通过正则表达式进行验证的原因:

  • 1234
  • 6543
  • 4567
  • 3333
  • 3331
  • 1333
  • 1239
  • 3219
  • 1789
  • 2543

虽然这些数字会通过:

  • 0443
  • 6690
  • 0420
  • 6798

换句话说,

  • 禁止上升和下降模式(1234,4321)。
  • 禁止输入数字4次的重复模式(1111,3333)。
  • 没有相同的三个连续数字。例如。因为111不行。
  • 没有三个数字的连续上升或下降。例如。如 321不行。

感谢。

1 个答案:

答案 0 :(得分:3)

描述

^(?!.*?(?: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

**要更好地查看图像,只需右键单击图像并在新窗口中选择视图

此正则表达式将执行以下操作:

  • 要求字符串长度为4个数字

  • 不允许相同的三个连续数字。例如。 111不行。

  • 不允许连续上升或下降三位或更多位数。例如678321不行。

实施例

现场演示

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

示例文字

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

样本匹配

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

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

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

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

解释

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)
----------------------------------------------------------------------