Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 45373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:47:55+00:00 2026-05-10T15:47:55+00:00

My goal here is to create a very simple template language. At the moment,

  • 0

My goal here is to create a very simple template language. At the moment, I’m working on replacing a variable with a value, like this:

This input:

The Web

Should produce this output:

The Web This Is A Test Variable

I’ve got it working. But looking at my code, I’m running multiple identical regexes on the same strings — that just offends my sense of efficiency. There’s got to be a better, more Pythonic way. (It’s the two ‘while’ loops that really offend.)

This does pass the unit tests, so if this is silly premature optimization, tell me — I’m willing to let this go. There may be dozens of these variable definitions and uses in a document, but not hundreds. But I suspect there’s obvious (to other people) ways of improving this, and I’m curious what the StackOverflow crowd will come up with.

def stripMatchedQuotes(item):     MatchedSingleQuotes = re.compile(r''(.*)'', re.LOCALE)     MatchedDoubleQuotes = re.compile(r''(.*)'', re.LOCALE)     item = MatchedSingleQuotes.sub(r'\1', item, 1)     item = MatchedDoubleQuotes.sub(r'\1', item, 1)     return item     def processVariables(item):     VariableDefinition = re.compile(r'<%(.*?)=(.*?)%>', re.LOCALE)     VariableUse = re.compile(r'<%(.*?)%>', re.LOCALE)     Variables={}      while VariableDefinition.search(item):         VarName, VarDef = VariableDefinition.search(item).groups()         VarName = stripMatchedQuotes(VarName).upper().strip()         VarDef = stripMatchedQuotes(VarDef.strip())         Variables[VarName] = VarDef         item = VariableDefinition.sub('', item, 1)      while VariableUse.search(item):         VarName = stripMatchedQuotes(VariableUse.search(item).group(1).upper()).strip()         item = VariableUse.sub(Variables[VarName], item, 1)      return item 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:47:56+00:00Added an answer on May 10, 2026 at 3:47 pm

    The first thing that may improve things is to move the re.compile outside the function. The compilation is cached, but there is a speed hit in checking this to see if its compiled.

    Another possibility is to use a single regex as below:

    MatchedQuotes = re.compile(r'(['\'])(.*)\1', re.LOCALE) item = MatchedQuotes.sub(r'\2', item, 1) 

    Finally, you can combine this into the regex in processVariables. Taking Torsten Marek’s suggestion to use a function for re.sub, this improves and simplifies things dramatically.

    VariableDefinition = re.compile(r'<%(['\']?)(.*?)\1=(['\']?)(.*?)\3%>', re.LOCALE) VarRepl = re.compile(r'<%(['\']?)(.*?)\1%>', re.LOCALE)  def processVariables(item):     vars = {}     def findVars(m):         vars[m.group(2).upper()] = m.group(4)         return ''      item = VariableDefinition.sub(findVars, item)     return VarRepl.sub(lambda m: vars[m.group(2).upper()], item)  print processVariables('<%'TITLE'='This Is A Test Variable'%>The Web <%'TITLE'%>') 

    Here are my timings for 100000 runs:

    Original       : 13.637 Global regexes : 12.771 Single regex   :  9.095 Final version  :  1.846 

    [Edit] Add missing non-greedy specifier

    [Edit2] Added .upper() calls so case insensitive like original version

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi here's the goal: to create an automatic bit of content that toggles open
The goal here is to take some list of coordinates, like [[1,2],[3,4],[7,1]] , then
Here is my ultimate goal... to take this xml file.. <?xml version=1.0?> <Songs> <Song>
Here's my ultimate goal in all of this. I have a viewcontroller with a
My over all goal is to upload a very simple ASP.NET web site created
Ok, so maybe my search syntax is wrong here, this could have very well
This problem is probably very simple to solve but it is not clear to
I'm very new to android. I would like to create a package that upon
My goal here is to make a button. I want the text to sit
My goal here is to open temp.php, explode by ### (line terminator), then by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.