Search code examples
pythonregexpython-3.xaugmented-assignment

Regex for augmented assignment operation using positive look-ahead or look-behind


I have used the following regular expression to search and match augmented assignment operators:

AUG_ASSIGN = r'\+=|\-=|\*=|@=|/=|%=|/@=|&=|\|=|\^=|\<\<=|\>\>=|\*\*=|//='

I have a hunch that it is possible to eliminate the multiple occurrences of '=' using positive look-ahead or positive look-behind. So, my first search was the grammar section in Python reference manual. Even there, '=' is occurring multiple times:

augop:           "+=" | "-=" | "*=" | "/=" | "%=" | "**="
               | ">>=" | "<<=" | "&=" | "^=" | "|="

Understandable, as standard BNF does not include positive look-ahead or look-behind.

Next, I went through the suggestions given by SO itself, as I typed this question and my searches did not get me near my intended meeting point. Is it possible to shrink the expression to something like:

AUG_ASSIGN = <set of all binary operators>(?=\=)

Solution

  • Put the single characters in a character set, and alternate with the other options which have two of the same character, so you can put them in a character set too (inside a group so that the same character can be matched again with a backreference). Put all of the above in a group, and end with =:

    (?:[+\-*@&/%^|^]|([*/><])\1)=
    

    https://regex101.com/r/JA84zS/4

    No lookaround needed.