正则表达式检查一个字符的 3 个或更多连续出现 [英] RegEx to check 3 or more consecutive occurrences of a character

查看:56
本文介绍了正则表达式检查一个字符的 3 个或更多连续出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查输入字符串以验证正确的文本.

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.但是,我不希望用户一起输入 3 位或更多位的数字.例如:12 是允许的,而 185 是不允许的.

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.

c.我不希望用户输入……"或--------"或aaaaaaaaaaaaaa"或bbbbbbbb"等字符串.

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.,()-]*$

它使用负前瞻(?!.*(.)\1{2}) 来确保没有任何字符的 3 组重复.

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

然后它使用正则表达式 ^[a-zA-Z0-9.,()-]*$ 来确保字符串仅由字母、数字、句点、逗号、括号组成和连字符.

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 链接

这篇关于正则表达式检查一个字符的 3 个或更多连续出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆