Regular Expression to match 3 or more Consecutive Sequential Characters and Consecutive Identical Characters

前端 未结 15 2279
北荒
北荒 2020-11-28 11:41

I need regular expressions to match the below cases.

  1. 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.
  2. 3 or more cons
15条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 12:22

    To my knowledge, the first case is indeed not possible. The regex engine doesn't know anything about the order of the natural numbers or the alphabet. But it's at least possible to differentiate between 3 or more numbers and 3 or more letters, for example:

    [a-z]{3,}|[A-Z]{3,}|\d{3,}
    

    This matches abcd, ABCDE or 123 but doesn't match ab2d, A5c4 or 12z, for example. According to this, the second case can be correctly given in a shorter version as:

      (\w)\1{2,}
    

提交回复
热议问题