开发者

RegEx to check 3 or more consecutive occurrences of a character

开发者 https://www.devze.com 2023-02-08 18:42 出处:网络
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 bracket ()

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

b. However, i dont want the users to enter a NUMBER with 3 or more digits together. eg: 12 is allowed while 185 is NOT.

开发者_Python百科c. I dont want the users to enter strings like "............." or "----------" or "aaaaaaaaaaaaaa" or "bbbbbbbb" etc.

Please suggest the regular expression for the same.


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


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

where \1 is a a backreference

0

精彩评论

暂无评论...
验证码 换一张
取 消