Search code examples
regexpython-3.xregex-groupbackreference

Python regex How to check with NOT condition for group backreference OR ignoring a group item in backreference


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.


Solution

  • 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.

    Regex demo

    Python demo

    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