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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:44:57+00:00 2026-06-13T18:44:57+00:00

With the code I’m using (r’ ‘, ‘ ‘), changes to a a a

  • 0

With the code I’m using (r' ', ' '), changes to a a a a(r' a', a' a'), when it should change to (r' a', ' '),. What’s a more natural way to do this? How do I do this with re.sub?

Current code, see here

for key, value in newgroupdict.items():
    try:
        newstr = newstr.replace(re.search(e, line).group(key), value)
    except:
        pass

Examples:

Expression: \s*(?:url)?\(r?["|'](?P<pattern>[^'"]+)["|'],\s*["|']?direct_to_template["|']?,\s*{["|']template["|']:\s*["|'](?P<template>[^'"]+)["|']}\),
String:     (r'^$', direct_to_template, {'template': 'home.html'}),
Dictionary: {u'pattern': u'^$abc', u'type': u'direct to template', u'template': u'home.html'}
Output:     (r'^$abc', direct_to_template, {'template': 'home.html'}),

Expression: \s*(?:url)?\(r?["|'](?P<pattern>[^'"]+)["|'],\s*["|']?(?P<view>[^'"]+)["|']?\),
String:     (r'^urls/', 'urls.views.urls'),
Dictionary: {u'pattern': u'^new_urls_pattern/', u'type': u'view', u'view': u'urls.views.urls'}
Output:     (r'^new_urls_patterns/', 'urls.views.urls'),

================= Incorrect Output ========================

Expression: \s*(?:url)?\(r?["|'](?P<pattern>[^'"]+)["|'],\s*["|']?(?P<view>[^'"]+)["|']?\),
String:     (r'^urls/', 'urls'),
Dictionary: {u'pattern': u'^new_urls_pattern/', u'type': u'view', u'view': u'urlsxyz'}
Incorrect Output:     (r'^urlsxyz/', 'urlsxyz'),
Correct Output:     (r'^urls/', 'urlsxyz'),

screenshot of djangogui

  • 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-06-13T18:44:59+00:00Added an answer on June 13, 2026 at 6:44 pm

    There are many ways to achieve this with a regexp, here’s one:

    In [23]: re.sub(r"'([^']+)'", r"'\1a'", "(r' ', ' '),", 1)
    Out[23]: "(r' a', ' '),"
    

    I’m not a good teacher and regexps are tricky to understand, still I’ll try to break this down to you:

    • Using re.sub, arguments used:
      • first argument is regexp,
      • second is replacement regexp,
      • third is subject, the string you want to work,
      • and finnaly is the number of times to apply the replacement,
    • Using the first regexp:
      1. ' will match a ' in your string subject,
      2. ( opens group \1, anything found between this and ) will be in group \1,
      3. [^'] matches any character except ',
      4. + means that the previous character class ([^']) can be repeated,
      5. ' will match the '
    • The replacement regexp says:
      • put ' to compensate for the replaced ' in 1.,
      • put whatever was matched in group \1, everything from 2. to 4., all non ' characters,
      • put ' to compensate for the last ' that is in the pattern regexp,

    Feel free to experiment with it, remove the count argument and stuff like that. But you will have to learn regexp at some point, so you should see that as a great opportunity to read the holy manual. Knowing to regexp will make you a much better programmer and give you power over text data.

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

Sidebar

Related Questions

Code to create new form instance of a closed form using form name I
Code for button: Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Code sample for my question: IList<TestDataAnimal> testDataFromDb = this.db.TestDataAnimals.Include(t => t.TestType).Include(t => t.Visit).Include(t =>
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Code: $(#telecomGrayscale, this).stop().animate({ top: '467px' }, { duration: 400 }).delay(800).queue(function() { $(#boxcaptionTelecom, this).stop().animate({ top:
Code sample should explain things: class A { B* pB; C* pC; D d;
code below. i'm tryind to obtain string answers like a1, c4 this is what
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Code in here: jsfiddle Demo I wanna to get the div#content height when 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.