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

前端 未结 15 2128
北荒
北荒 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:34

    I don't think you can use regex for the first case. The second case is easy though:

    Pattern pattern = Pattern.compile("([a-z\\d])\\1\\1", Pattern.CASE_INSENSITIVE);
    

    Since \\1 represents part matched by group 1 this will match any sequence of three identical characters that are either within the range a-z or are digits (\d).

    0 讨论(0)
  • 2020-11-28 12:36

    for the second question:

    \\b([a-zA-Z0-9])\\1\\1+\\b
    

    explanation:

    \\b               : zero-length word boundary
      (               : start capture group 1
        [a-zA-Z0-9]   : a letter or a digit
      )               : end group
      \\1             : same character as group 1
      \\1+            : same character as group 1 one or more times
    \\b               : zero-length word boundary
    
    0 讨论(0)
  • 2020-11-28 12:41

    All put together:

    ([a-zA-Z0-9])\1\1+|(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

    3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.

    (abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

    3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.

    ([a-zA-Z0-9])\1\1+

    https://regexr.com/4727n

    This also works:

    (?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2,}\d|(?:a(?=b)|b(?=c)|c(?=d)|d(?=e)|e(?=f)|f(?=g)|g(?=h)|h(?=i)|i(?=j)|j(?=k)|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o(?=p)|p(?=q)|q(?=r)|r(?=s)|s(?=t)|t(?=u)|u(?=v)|v(?=w)|w(?=x)|x(?=y)|y(?=z)){2,}[[:alpha:]])|([a-zA-Z0-9])\1\1+

    https://regex101.com/r/6fXC9u/1

    0 讨论(0)
提交回复
热议问题