Problem:- A number is valid if the alternate numbers are different. In case if the alternate numbers are same and the in-between number is also same then they are valid again
example:-
123456 :- This number is valid as we don't have any alternate number as same, all are different
110100:- This number is invalid as in 010 alternate numbers are same and in-between is different
110000 :- this number is valid as in 000 alternate number though are same in-between is also same
What I tried
import re
st="1101010"
#st="110000"
re.findall(r"(\d)[^\1]&[\d]\1", st)
I was trying to not the group item by back referencing and using an AND condition, but this doesn't work.
You can use the regular expression
r"(\d)(?!\1)\d\1"
Strings that match this regex contain a string aba
where a
is any character and b
is any character other than a
.
Python's regex engine performs the following operations.
(\d) # match a digit and save to cap grp 1
(?!\1) # the next char cannot be the content of cap grp 1
\d # match a digit
\1 # match the content of cap grp 1