RegEx to check 3 or more consecutive occurrences of a character

后端 未结 2 832
逝去的感伤
逝去的感伤 2021-01-13 08:36

I want to check an input string to validate a proper text.

a. I want the users to allow to writer alphanumeric characters including period, comma, hyphen and round b

相关标签:
2条回答
  • 2021-01-13 09:08

    You can use the regex:

    (?!.*(.)\1{2})^[a-zA-Z0-9.,()-]*$
    

    It uses the negative lookahead (?!.*(.)\1{2}) to ensure that there is no group of 3 repetitions of any of the characters.

    Then it uses the regex ^[a-zA-Z0-9.,()-]*$ to ensure that the string is made of just alphabets, numbers, period, comma, parenthesis and hyphen.

    Rubular link

    0 讨论(0)
  • 2021-01-13 09:09

    Most regex libs support the folloing: /(.)\1{2,}/

    where \1 is a a backreference

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