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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:53:17+00:00 2026-05-20T15:53:17+00:00

new to python. This is probably simple but I haven’t found an answer. rndStr

  • 0

new to python. This is probably simple but I haven’t found an answer.

rndStr = "20101215"
rndStr2 = "20101216"
str = "Looking at dates between 20110316 and 20110317"
outstr = re.sub("(.+)([0-9]{8})(.+)([0-9]{8})",r'\1'+rndStr+r'\2'+rndStr2,str)

The output I’m looking for is:

Looking at dates between 20101215 and 20101216

But instead I get:

P101215101216

The values of the two rndStr’s doesn’t really matter. Assume its random or taken from user input (I put static vals here to keep it simple). Thanks for any help.

  • 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-20T15:53:18+00:00Added an answer on May 20, 2026 at 3:53 pm

    Your backreferences are ambiguous. Your replacement string becomes

    \120101215\220101216
    

    which is two rather large numbers to be backreferencing 🙂

    To solve it, use this syntax:

    r'\g<1>'+rndStr+r'\g<2>'+rndStr2 
    

    You also have too many sets of parentheses (or “brackets” if you speak British English like me:) – you don’t need parentheses around the [0-9]{8} parts which you’re not backreferencing:

    re.sub("(.+)[0-9]{8}(.+)[0-9]{8}",...
    

    should be sufficient.

    (And, as noted elsewhere, don’t use str as a variable name. Unless you want to spend ages debugging why str.replace() doesn’t work anymore. Not that I ever did that once… noooo. 🙂

    so the whole thing becomes:

    import re
    rndStr = "20101215"
    rndStr2 = "20101216"
    s = "Looking at dates between 20110316 and 20110317"
    outstr = re.sub("(.+)[0-9]{8}(.+)[0-9]{8}", r'\g<1>'+rndStr+r'\g<2>'+rndStr2, s) 
    print outstr
    

    Producing:

    Looking at dates between 20101215 and 20101216
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a rudimentary question, but I'm new to threaded programming in Python
Alright, this is probably a really silly question but I am new to Python/Django
Pretty simple question (probably) but I guess since im new to python im not
I am new to python and havent found anything which suggests this is probably
I'm brand new to Django, so the answer to this is probably very simple.
Brand new to Python (and programming in general), if this is simple and/or answered
This one is probably a simple one, but I could not find an example
I'm new to python and to django so this question will probably be easy
Very new to python and can't understand why this isn't working. I have a
I'm pretty new at python and I was wondering if this: def func(self, foo):

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.