import re
regex = re.compile(r"(?x)(?-i:class)\s+[A-Za-z_$][\w$]*\s*\{", flags=re.MULTILINE)
test_str = ("class Entity\n"
"{\n"
" constructor(game,imageURL)\n"
" {\n"
" this.game = game;\n"
" this.sprite = new Sprite(this.game.c,imageURL);\n"
" \n"
" }\n"
" \n"
" \n"
" draw(depth,offset,textureSize,textureCoordinates,offsetX=0)\n"
" {\n"
" let scaleFactors = {\n"
" 1:1,\n"
" 2:0.65,\n"
" 3:0.45\n"
" };\n"
" let scaleFactor = scaleFactors[depth];\n"
" \n"
" let x = Math.floor(offset.x + textureCoordinates[0] + (textureSize[0] / 2) - (this.sprite.image.width * scaleFactor / 2));\n"
" let y = Math.floor(offset.y + textureCoordinates[1] + (textureSize[1] / 2) - (this.sprite.image.height * scaleFactor) + 6 * scaleFactor);\n"
" \n"
" x = x + offsetX * scaleFactor;\n"
" \n"
" let width = Math.floor(this.sprite.image.width * scaleFactor);\n"
" let height = Math.floor(this.sprite.image.height * scaleFactor);\n"
" \n"
" let entityFades = {\n"
" 1:1,\n"
" 2:0.5,\n"
" 3:0.1\n"
" }\n"
" let filter = '';\n"
" if(this.game.level.entities.fadeType === 'darken')\n"
" {\n"
" filter += 'brightness('+entityFades[depth]+')';\n"
" }\n"
" \n"
" this.sprite.draw(\n"
" x,\n"
" y,\n"
" width,\n"
" height,\n"
" 0,\n"
" 0,\n"
" this.sprite.image.width,\n"
" this.sprite.image.height,\n"
" filter\n"
" );\n"
" }\n"
"}")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html