问题
I'm trying to make a single pattern that will validate an input string. The validation rule does not allow any character to be repeated more that 3 times in a row.
For example:
Aabcddee
- is valid.
Aabcddde
- is not valid, because of 3 d chracters.
The goal is to provide a RegExp pattern that could match one of above examples, but not both. I know I could use back-references such as ([a-z])\1{1,2}
but this matches only sequential characters. My problem is that I cannot figure out how to make a single pattern for that. I tried this, but I don't quite get why it isn't working:
^(([a-z])\1{1,2})+$
Here I try to match any character that is repeated 1 or 2 times in the internal group, then I match that internal group if it's repeated multiple times. But it's not working that way.
Thanks.
回答1:
From your question I get that you want to match
- only strings consisting of chars from
[A-Za-z]
AND - only strings which have no sequence of the same character with a length of 3 or more
Then this regexp should work:
^(?:([A-Za-z])(?:(?!\1)|\1(?!\1)))+$
(Example in perl)
回答2:
To check that the string does not have a character (of any kind, even new line) repeated 3 times or more in a row:
/^(?!.*(.)\1{2})/s
You can also check that the input string does NOT have any match to this regex. In this case, you can also know the character being repeated 3 times or more in a row. Notice that this is exactly the same as above, except that the regex inside the negative look-ahead (?!pattern)
is taken out.
/^.*(.)\1{2}/s
If you want to add validation that the string only contains characters from [a-z]
, and you consider aaA
to be invalid:
/^(?!.*(.)\1{2})[a-z]+$/i
As you can see i
flag (case-insensitive) affect how the text captured is compared against the current input.
Change +
to *
if you want to allow empty string to pass.
If you want to consider aaA
to be valid, and you want to allow both upper and lower case:
/^(?!.*(.)\1{2})[A-Za-z]+$/
At first look, it might seem to be the same as the previous one, but since there is no i
flag, the text captured will not subject to case insensitive matching.
Below is failed answer, you can ignore it, but you can read it for fun.
You can use this regex to check that the string does not have 3 repeated character (of any kind, even new line).
/^(?!.*(.)(?:.*\1){2})/s
You can also check that the input string does NOT have any match to this regex. In this case, you can also know the character being repeated more than or equal to 3 times. Notice that this is exactly the same as above, except that the regex inside the negative look-ahead (?!pattern)
is taken out.
/^.*(.)(?:.*\1){2}/s
If you want to add validation that the string only contains characters from [a-z]
, and you consider aaA
to be invalid:
/^(?!.*(.)(?:.*\1){2})[a-z]+$/i
As you can see i
flag (case-insensitive) affect how the text captured is compared against the current input.
If you want to consider aaA
to be valid, and you want to allow both upper and lower case:
/^(?!.*(.)(?:.*\1){2})[A-Za-z]+$/
At first look, it might seem to be the same as the previous one, but since there is no i
flag, the text captured will not subject to case insensitive matching.
来源:https://stackoverflow.com/questions/14375402/regexp-how-to-match-a-string-that-doesnt-have-any-character-repeated-3-times