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 4602248
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:57:15+00:00 2026-05-21T23:57:15+00:00

So, I am successfully matching and extracting some special tagged text using the following

  • 0

So, I am successfully matching and extracting some special tagged text using the following regular expression:

theString = u"Var 1 value: %%v:123453%%, Var 2 value: %%v:984561%%, Var 3 value: %%v:123456%%"
p = re.compile("\%%v:([0-9]*)%%")
theIds = p.findall(theString)

That returns

[u'123453', u'984561', u'123456']

which is exactly what I need. Next, I need to replace those with some looked up value, so what I’d like to get next is this:

[u'Var 1 value: ', u', Var 2 value: ', u', Var 3 value: ']

So that I can glue those strings together with the looked up values from the first list, resulting in a string that looks something like this:

u”Var 1 value: Some Value, Var 2 value: 837, Var 3 value: more stuff”

Or, if there’s a better way to do the replacement I’m all ears.

Thanks in advance!

  • 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. Editorial Team
    Editorial Team
    2026-05-21T23:57:16+00:00Added an answer on May 21, 2026 at 11:57 pm

    Use a replacement function to insert arbitrary substitutions. See the re.sub documentation for how the function works. Here is an example:

    values = {
        u'123453': u'Some Value',
        u'984561': u'837',
        u'123456': u'more stuff',
    }
    
    def insertLookup(matchobj):
        return values[matchobj.group(1)]
    
    theString = u"Var 1 value: %%v:123453%%, Var 2 value: %%v:984561%%, Var 3 value: %%v:123456%%"
    p = re.compile("\%%v:([0-9]*)%%")
    newString = p.sub(insertLookup, theString)
    
    print newString
    u"Var 1 value: Some Value, Var 2 value: 837, Var 3 value: more stuff"
    

    The insertLookup function will be called for each match, and is passed a MatchObject. We then use the matched value (u'123453', etc.) to look up the replacement value, which then is inserted into newString instead of the matched string.

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

Sidebar

Related Questions

can some tell how can i write regular expression matching abc.com.ae or abc.net.af or
When using the find command, why is it that the following will successfully ignore
I've successfully setup my FilteringSelect to query my server for items dynamically using the
I am trying to write a regular expression to match file paths with the
I have a stylesheet, which successfully applies itself to any text wrapped with a
I have a web service application that I deployed successfully on Apache Tomcat using
Consider the following (nasty) code: /// <summary> /// Calls matching process error code on
I successfully created database on my local server using Code First, migrated it to
I successfully inserted some data into my sqlite database(I confirmed this by printing out
Successfully used jquery-infinite-carousel in the past but for my current project I need it

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.