regexregex-lookaroundsregex-negation

Regex matching with multiple negative look behind conditions


I would like to create a regex pattern that does not match multiple negative look behind patterns.

But if I try to compile a pattern like

import re
split_pattern = re.compile("(?<!AA|BAB|CAC|JHGS)[1-3]\s*(?=[A-Z])")

I get the error error: look-behind requires fixed-width pattern

Do you know a work-around this problem?


Solution

  • You should just be able to split up the negative lookbehind into multiple lookbehinds like so

    (?<!AA)(?<!BAB)(?<!CAC)(?<!JHGS)[1-3]\s*(?=[A-Z])
    

    Edit: As mentioned below in the comments, since BAB/CAC are the same size you could take those together

    (?<!AA)(?<!BAB|CAC)(?<!JHGS)[1-3]\s*(?=[A-Z])