# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"{.+|(?<=var)\s+([^\s]+)\s*=\s*['\"]([^'\"]+?)['\"];"
test_str = ("//stuff\n"
"var RootJsURL = \"/trunk/js/portal\"; //The root of the OpenMap Application\n"
"var RootRestUrl = \"/trunk/rest\"; //root of the restful services directory\n"
"var stripePublicKey = 'pk_test_rAYTGcpiFtrXvXpYic4C0Mt0';//the public key for your stripe account\n\n"
"//global functions for any tool to use\n"
"var showErrorPopup = function(title, message) {\n"
" Ext.MessageBox.show({\n"
" title: title,\n"
" msg: \"Error: \" + message,\n"
" buttons: Ext.MessageBox.OK,\n"
" icon: Ext.Msg.ERROR\n"
" });\n"
"};\n\n"
"/**\n"
" * checks if there is an error, if so display it\n"
" * @param obj the response json to check for an error\n"
" * @param title the title of the error pop up box\n"
" * @returns {boolean} true if there is an error, false otherwise\n"
" */\n"
"var checkResponseForError = function(obj, title) {\n"
" if (obj.error) {\n"
" if (obj.loggedIn) {\n"
" showErrorPopup(title, obj.error);\n"
" return true;\n"
" }\n"
" else {\n"
" countdown.modal.show();\n"
" }\n"
" }\n"
" return false;\n"
"};\n\n\n"
"//The configure object that puts it all together\n"
"var Configure = {\n"
" LBAR:[],\n"
" TBAR:[],\n"
" BBAR:[],\n"
" RBAR:[],\n"
" centerbar:null,\n"
" /**\n"
" * List of window resize functions\n"
" */\n"
" ResizeEvents:[],\n"
" /**\n"
" * Gets the working size of the main panel\n"
" * @returns {number}\n"
" */\n"
" GetClientHeight:function(){\n"
" //var body = document.body;\n"
" //var html = document.documentElement;\n"
" var bannerHeight = document.getElementById(\"bannerimg\").height;\n"
" //return Math.max(body.scrollHeight, body.offsetHeight,html.clientHeight, html.scrollHeight, html.offsetHeight)-bannerHeight-5;//Buffer of 5px --> need to fix and make it universal\n"
" return Ext.getBody().getViewSize().height -bannerHeight-5;\n"
" },\n"
" /**\n"
" * Configure all global variables\n"
" */\n"
" Globals:function(){\n"
" //Create the dynamic content panel\n"
" this.centerbar = Ext.create('Ext.panel.Panel',{\n"
" region:'center',\n"
" autoScroll:true\n"
" });\n"
" },\n"
" /**\n"
" * Assembles all tools into the main panel before drawing\n"
" */\n"
" MainPanel:function(){\n"
" \n"
" //If LBar and BBar are empty, don't bother rendering them --> set to null\n"
" var topArray = [];\n"
" var bottomArray = []; \n"
" \n"
" if(this.TBAR.length >0){\n"
" topArray = topArray.concat(this.TBAR);\n"
" }\n"
" if(this.BBAR.length >0){\n"
" bottomArray = bottomArray.concat(this.BBAR);\n"
" }\n\n"
" //Top bar --> Persistant tools\n"
" var TopBar = Ext.create('Ext.panel.Panel',{\n"
" region: 'north',\n"
" width:'100%',\n"
" layout:{\n"
" type:'hbox',\n"
" align: 'left',\n"
" pack: 'end'\n"
" },\n"
" margin: '0 0 0 0',\n"
" items: topArray\n"
" });\n"
" //Bottom bar --> usually pertinant info\n"
" var BottomBar = Ext.create('Ext.panel.Panel',{\n"
" region: 'south',\n"
" width:'100%',\n"
" layout:'hbox',\n"
" margin: '0 0 0 0',\n"
" items: bottomArray\n"
" });\n"
" \n"
" //Side tool panel --> Extensible tools\n"
" var toolbar = Ext.create('Ext.panel.Panel',{\n"
" region: 'west',\n"
" width: 200,\n"
" minWidth: 200,\n"
" maxWidth: 400,\n"
" collapsible: false,\n"
" animCollapse:true,\n"
" hideCollapseTool: true,\n"
" floatable: true,\n"
" split: false,\n"
" margin: '0 0 0 0', \n"
" layout:{\n"
" type: 'vbox'\n"
" },\n"
" items:this.LBAR\n"
" });\n"
" \n"
" //Right tool panel --> Extensible tools\n"
" var Rightbar = Ext.create('Ext.panel.Panel',{\n"
" region: 'east',\n"
" title:'Portal Information',\n"
" width: 200,\n"
" minWidth: 200,\n"
" maxWidth: 400,\n"
" collapsible: true,\n"
" animCollapse:true,\n"
" hideCollapseTool: true,\n"
" floatable: true,\n"
" split: false,\n"
" margin: '0 0 0 0', \n"
" layout:{\n"
" type: 'fit'\n"
" },\n"
" items:this.RBAR\n"
" });\n"
" if(!Rightbar.getCollapsed()){\n"
" Rightbar.toggleCollapse(); \n"
" } \n"
" \n"
" var cb = this.centerbar;\n\n"
" //Overall Panel --> Contains everything\n"
" MainPanel = Ext.create('Ext.panel.Panel',{\n"
" layout: {\n"
" type:'border',\n"
" padding: '0 0 0 0',\n"
" margin: '0 0 0 0'\n"
" },\n"
" width: '100%',\n"
" height:Configure.GetClientHeight(),//Buffer of 3px --> need to fix and make it universal\n"
" items:[toolbar,cb,TopBar,BottomBar,Rightbar],\n"
" renderTo: Ext.getBody()\n"
" }); \n\n"
" //Window resize event\n"
" Ext.EventManager.onWindowResize(function () {\n"
" var width = '100%';\n"
" var height = Configure.GetClientHeight();\n\n"
" MainPanel.setSize(width, height);\n"
" for(var i = 0; i< Configure.ResizeEvents.length;i++){\n"
" Configure.ResizeEvents[i]();\n"
" }\n"
" });\n"
" },\n\n"
" /**\n"
" * Adds an element to the left hand side of the page\n"
" * @param {Array} elements\n"
" * @return void \n"
" */\n"
" AddToLeftbar:function(elements){\n"
" this.LBAR = this.LBAR.concat(elements);\n"
" },\n"
" /**\n"
" * Adds an element to the top of the page\n"
" * @param {Array} elements\n"
" * @return void \n"
" */\n"
" AddToTopbar:function(elements){\n"
" this.TBAR = this.TBAR.concat(elements);\n"
" },\n"
" /**\n"
" * Adds an element to the bottom of the page\n"
" * @param {Array} elements\n"
" * @return void \n"
" */\n"
" AddToBottombar:function(elements){\n"
" this.BBAR = this.BBAR.concat(elements);\n"
" },\n"
" /**\n"
" * Adds an element to the right hand side of the page\n"
" * @params {Array} elements\n"
" * @return void \n"
" */\n"
" AddToRightbar:function(elements){\n"
" this.RBAR = this.RBAR.concat(elements);\n"
" },\n"
" /**\n"
" * Replaces the center panel content\n"
" * @params {Ext.Component} element\n"
" * @return void \n"
" */\n"
" UpdateContent:function(element){\n"
" WindowManager.Clear();\n"
" this.centerbar.removeAll(false);\n"
" this.centerbar.add(element);\n"
" },\n"
" /**\n"
" * Adds a window resize event\n"
" * @params {function} func\n"
" * @return void \n"
" */\n"
" AddResizeEvent:function(func){\n"
" this.ResizeEvents = this.ResizeEvents.concat(func);\n"
" }\n"
"}\n")
matches = re.finditer(regex, test_str, re.DOTALL)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.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