Reputation:
I am struggling to write regex pattern.
I want, It must NOT have or more consecutive repeated digits.
5133-3367-8912-3456
so it should return false, Consecutive digits are repeating 4 or more times
5123-4567-8912-3456
and it should return True
my code now: re.match('[0-9]{4}', string)
I can do with many way in python but i want to do with regex as my case is very different.
Can anyone please help me in this case?
Upvotes: 1
Views: 120
Reputation: 58431
If you remove all hyphens first, you can use a positive lookahead to search for a repeating pattern for a specified number of times.
if re.search(r"(\d)(?=\1{3})", string):
# Successful match -> This would be your false case
else:
# Match attempt failed -> This would be your true case
Upvotes: 0
Reputation: 784938
You may use this regex in python with a negative lookahead:
^(?![\d-]*(\d)(?:-?\1){3})\d+(?:-\d+)*$
RegEx Details:
^
: Start(?![\d-]*(\d)(?:-?\1){3})
: Negative lookahead to fail the match when we find 4 repeats of the same digit optionally preceded by a single -
\d+(?:-\d+)*
: Match digits sets delimited by -
$
: EndUpvotes: 1