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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:23:48+00:00 2026-06-15T06:23:48+00:00

I have always had hard time understanding regular expressions. With the help of web

  • 0

I have always had hard time understanding regular expressions. With the help of web searches i have always managed to pull through somehow. Guess i have never bothered to really learn then. Sorry.

But i need help with them yet again.

I have a dict like

d = {'account_id':a_value,'group_id':g_value,'other_id':o_value }

And i have bunch of strings like:

s1 = r'^settings/usergroups/(?P<group_id>\d+)/cargroups/$'
s2 = r'^settings/usergroups/(?P<group_id>\d+)/other/(?P<other_id>\d+)/$',
s3 = r'^settings/account/(?P<account_id>\d+)/other/(?P<other_id>\d+)/$',

How can O replace the (?P< group_id >\d+), (?P< account_id >\d+), (?P< other_id >\d+) in the strings with matching values from dict?

  • 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-15T06:23:49+00:00Added an answer on June 15, 2026 at 6:23 am

    Regular expressions can contain nested parentheses.

    However, Python regular expressions can not match strings containing arbitrarily
    deep nested parentheses
    in a way that respects the nested structure. (It is possible using Perl’s recursive regular expressions.)

    So, if your use case involves strings that DO NOT contain nested paretheses,
    then the following suffices, but note carefully the undesired extra parenthesis in the last result below:

    import re
    d = {'account_id':'a_value','group_id':'g_value','other_id':'o_value' }
    
    tests = (r'^settings/usergroups/(?P<group_id>\d+)/cargroups/$',
             r'^settings/usergroups/(?P<group_id>\d+)/other/(?P<other_id>\d+)/$',
             r'^settings/account/(?P<account_id>\d+)/other/(?P<other_id>\d+)/$',
             r'^settings/usergroups/(?P<group_id>(\d+|\w))/other/(?P<other_id>\d+)/$'
             )
    metapat = r'\(\?P<(.*?)>.*?\)'
    for t in tests:
          result = re.sub(metapat, r'{\1}', t)
          if result:
                print(result.format(**d))
    

    yields

    ^settings/usergroups/g_value/cargroups/$
    ^settings/usergroups/g_value/other/o_value/$
    ^settings/account/a_value/other/o_value/$
    ^settings/usergroups/g_value)/other/o_value/$
    

    If you do need to parse nested parentheses, then you’ll need a different parser than re. Pyparsing, for example, can handle nested expressions.

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

Sidebar

Related Questions

I have always had a hard time understanding the real value of Interfaces when
I've always had a hard time understanding the full concepts of MVC, MVP, MVVM,
I have modified a working Windows service that had always been starting beforehand. After
I have always enabled integrated security on my web apps inside IIS with the
I've had a hard time trying to find good examples of how to manage
I have always had an interest in coding, and a while back a started
this is something I have always had confusion about I never can seem to
Although the Title might not make sense (I had a hard time describing this
We have always had languages that were preferable to be used in a particular
I have always and will continue to always close an HTML input such as

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.