I'm trying to make a regular expression that detects if a string has at least 5 characters and at least one of them is repeated, I'm trying something like this:
"([\d]?[\d]){5,}"
But I think I'm misusing this expression:
(?[\d])
The accepted strings would be:
123451
12321
111111
The rejected ones would be:
1232
123456
123456789
The proposed solution uses two features of regular expressions
backreference
andnon-consuming
backreference
allows a capture to be used in the evaluation of the expression. For this particular case, the portion of the pattern that makes use of this feature is(.)(.)*\\1
that it defines(.)
any character(.)
followed by any zero or more characters(.)*
and that the capture of the first group is repeated\1
, which is the first character that was capturednon-consuming
allows defining that a group does not consume the evaluation line; that is, the evaluation of the next pattern of the regular expression continues to evaluate where the capturing of the pattern that was declared asnon-consuming
the group that is declared asnon-consuming
defined by enclosing it between(?=
and began)
. For the particular case of the question, compliance with at least 5(?=[\d]{5,})
mode digits is first defined.non-consuming
The resulting regular expression pattern proposed for the question is
(?=[\d]{5,})(?=(.)(.)*\1)
that it indicates that the capture must first have 5 or more digits and that in the same capture any character must be repeated at least onceBelow is a program
java
that uses the proposed regular expression pattern evaluated against the input strings that is exposed in the question for thelookingAt()
class methodPattern
. The required characters are assumed to be digits, considering that the cases presented in the question are limited to digits; but in any case you can easily change the character set if you want to extend it.