# [Solved] Python regex with look behind and alternatives

I want to have a regular expression that finds the texts that are “wrapped” in between “HEAD or HEADa” and “HEAD. That is, I may have a text that starts with the first word as HEAD or HEADa and the following “heads” are of type HEAD.

1. HEADnn text...text...HEAD nn text....text HEADnn text....text .....
2. HEADann text...text...HEAD nn text....text HEADnn text....text .....

I want only to capture the text that are in between the “heads” therefore I have a regex with look behind and look ahead expressions looking for my “heads”. I have the following regex:

var = "HEADa", "HEAD"

my_pat = re.compile(r"(?<=^b"+var[0]+r"|"+var[1]+r"b) w*ss(.*?)(?=b"+var[1] +r"b)",re.DOTALL|re.MULTILINE)


However, when I try to execute this regex, I am getting an error message saying that I cannot have variable length in the look behind expression. What is wrong with this regex?