RegEx用于检查3个或更多连续出现的字符

时间:2022-08-09 20:13:34

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.

湾但是,我不希望用户一起输入3位或更多位数的NUMBER。例如:允许12,而185不允许。

c. I dont want the users to enter strings like "............." or "----------" or "aaaaaaaaaaaaaa" or "bbbbbbbb" etc.

C。我不希望用户输入“.............”或“----------”或“aaaaaaaaaaaaaa”或“bbbbbbbb”等字符串。

Please suggest the regular expression for the same.

请建议相同的正则表达式。

2 个解决方案

#1


1  

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.

它使用负前瞻(?!。*(。)\ 1 {2})来确保不存在任何字符的3个重复组。

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.

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

Rubular link

#2


0  

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

大多数正则表达式库支持以下内容:/(.)\1{2,}/

where \1 is a a backreference

其中\ 1是反向引用

#1


1  

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.

它使用负前瞻(?!。*(。)\ 1 {2})来确保不存在任何字符的3个重复组。

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.

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

Rubular link

#2


0  

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

大多数正则表达式库支持以下内容:/(.)\1{2,}/

where \1 is a a backreference

其中\ 1是反向引用