# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"_gpfErrorDeclare\(\"([a-zA-Z\\]+)\", {\n((?:.*\n)*)\s*}\)"
test_str = ("/**\n"
" * @file Error base class\n"
" * @since 0.1.5\n"
" */\n"
"/*#ifndef(UMD)*/\n"
"\"use strict\";\n"
"/*global _gpfExtend*/ // gpf.extend\n"
"/*global _gpfIgnore*/ // Helper to remove unused parameter warning\n"
"/*global _gpfObjectForEach*/ // Similar to [].forEach but for objects\n"
"/*global _gpfStringCapitalize*/ // Capitalize the string\n"
"/*global _gpfStringReplaceEx*/ // String replacement using dictionary map\n"
"/*exported _gpfErrorDeclare*/ // Declare new gpf.Error names\n"
"/*#endif*/\n\n"
"/**\n"
" * GPF Error class\n"
" *\n"
" * @constructor\n"
" * @alias gpf.Error\n"
" * @since 0.1.5\n"
" */\n"
"var _GpfError = gpf.Error = function () {};\n\n"
"_GpfError.prototype = new Error();\n"
"_gpfExtend(_GpfError.prototype, /** @lends gpf.Error.prototype */ {\n\n"
" constructor: _GpfError,\n\n"
" /**\n"
" * Error code\n"
" *\n"
" * @readonly\n"
" * @since 0.1.5\n"
" */\n"
" code: 0,\n\n"
" /**\n"
" * Error name\n"
" *\n"
" * @readonly\n"
" * @since 0.1.5\n"
" */\n"
" name: \"Error\",\n\n"
" /**\n"
" * Error message\n"
" *\n"
" * @readonly\n"
" * @since 0.1.5\n"
" */\n"
" message: \"\",\n\n"
" /**\n"
" * Build message by substituting context variables\n"
" *\n"
" * @param {Object} context Dictionary of named keys\n"
" * @since 0.1.5\n"
" */\n"
" _buildMessage: function (context) {\n"
" var replacements;\n"
" if (context) {\n"
" replacements = {};\n"
" _gpfObjectForEach(context, function (value, key) {\n"
" replacements[\"{\" + key + \"}\"] = value.toString();\n"
" });\n"
" this.message = _gpfStringReplaceEx(this.message, replacements);\n"
" }\n"
" }\n\n"
"});\n\n"
"function _gpfErrorFactory (code, name, message) {\n"
" function NewErrorClass (context) {\n"
" this._buildMessage(context);\n"
" }\n"
" NewErrorClass.prototype = new _GpfError();\n"
" _gpfExtend(NewErrorClass.prototype, {\n"
" code: code,\n"
" name: name,\n"
" message: message\n"
" });\n"
" // constructor can't be enumerated with wscript\n"
" NewErrorClass.prototype.constructor = NewErrorClass;\n"
" _GpfError[_gpfStringCapitalize(name)] = NewErrorClass;\n"
" return function (context) {\n"
" throw new NewErrorClass(context);\n"
" };\n"
"}\n\n"
"/**\n"
" * Generates an error class\n"
" *\n"
" * @param {Number} code Error code\n"
" * @param {String} name Error name\n"
" * @param {String} message Error message\n"
" * @return {Function} New error class\n"
" * @gpf:closure\n"
" * @since 0.1.5\n"
" */\n"
"function _gpfGenenerateErrorFunction (code, name, message) {\n"
" var result = _gpfErrorFactory(code, name, message);\n"
" result.CODE = code;\n"
" result.NAME = name;\n"
" result.MESSAGE = message;\n"
" return result;\n"
"}\n\n"
"// Last allocated error code\n"
"var _gpfLastErrorCode = 0;\n\n"
"/**\n"
" * Declare error messages.\n"
" * Each source declares its own errors.\n"
" *\n"
" * @param {String} source Source name\n"
" * @param {Object} dictionary Dictionary of error name to message\n"
" * @since 0.1.5\n"
" */\n"
"function _gpfErrorDeclare (source, dictionary) {\n"
" _gpfIgnore(source);\n"
" _gpfObjectForEach(dictionary, function (message, name) {\n"
" var code = ++_gpfLastErrorCode;\n"
" gpf.Error[\"CODE_\" + name.toUpperCase()] = code;\n"
" gpf.Error[name] = _gpfGenenerateErrorFunction(code, name, message);\n"
" });\n"
"}\n\n"
"_gpfErrorDeclare(\"error\", {\n"
" /**\n"
" * ### Summary\n"
" *\n"
" * Method or function is not implemented\n"
" *\n"
" * ### Description\n"
" *\n"
" * This error is used to flag methods or functions that are not yet implemented.\n"
" * @since 0.1.5\n"
" */\n"
" notImplemented:\n"
" \"Not implemented\",\n\n"
" /**\n"
" * ### Summary\n"
" *\n"
" * Method is abstract\n"
" *\n"
" * ### Description\n"
" *\n"
" * This error is used to implement abstract methods. Mostly used for interfaces.\n"
" * @since 0.1.5\n"
" */\n"
" abstractMethod:\n"
" \"Abstract method\",\n\n"
" /**\n"
" * ### Summary\n"
" *\n"
" * An assertion failed\n"
" *\n"
" * ### Description\n"
" *\n"
" * This error is triggered when an assertion fails\n"
" *\n"
" * @see {@link gpf.assert}\n"
" * @see {@link gpf.asserts}\n"
" * @since 0.1.5\n"
" */\n"
" assertionFailed:\n"
" \"Assertion failed: {message}\",\n\n"
" /**\n"
" * ### Summary\n"
" *\n"
" * Method or function was called with an invalid parameter\n"
" *\n"
" * ### Description\n"
" *\n"
" * This error is used when a parameter is invalid\n"
" * @since 0.1.5\n"
" */\n"
" invalidParameter:\n"
" \"Invalid parameter\"\n"
"});\n")
matches = re.search(regex, test_str)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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