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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:03:04+00:00 2026-06-18T08:03:04+00:00

I want to build a regular expression based on a template and a set

  • 0

I want to build a regular expression based on a template and a set of predefined blocks, and using string.Template for the substitution.

For example:

  • template: /data/${year}_${month}_${day}/${year}${month}${day}_${type}_${id}.dat
  • blocks:
    • day: (?P<day>\d{2})
    • month: (?P<month>\d{2})
    • year: (?P<year>\d{4})
    • type: (?P<typechar>[BDPCLNIYSQJ])
    • id: (?P<id>\d{8})

>>> string.Template(template).safe_substitute(blocks)

/data/(?P<year>\d{4})_(?P<month>\d{2})_(?P<day>\d{2})/(?P<year>\d{4})(?P<month>\d{2})(?P<day>\d{2})_(?P<typechar>[BDPCLNIYSQJ])_(?P<id>\d{8}).dat

The problem is with duplicated name groups, which are not accepted in the regular expression.

I’m looking either for a way to correct the template (before or after the substitution), a way to trick re to swallow the duplicates, or a complete new approach to the problem.

  • 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-18T08:03:06+00:00Added an answer on June 18, 2026 at 8:03 am

    After following a friend’s advice, I found a way to achieve the desired result.

    The idea is to modify the template string to eliminate duplicate vars before substituting the regex blocks. In fact it’s not removing the duplicates but replacing them with a reference to the first one with the (?P=name) syntax. This way you force the contents to be the same everywhere you use that block.

    I will assume the regex groupname is the same as the template block name. This is not true in the question example, but it can be changed without any problem.

    To transform the duplicates I use the following function:

    >>> def remove_duplicate_blocks(template):
            regex = '\$\{([\w]+)\}'
            def alt_seen(matchobj):
                x = matchobj.group(1)
                if x not in seen and not seen_add(x): return '${%s}' % x
                else: return '(?P=%s)' % x
            seen = set()
            seen_add = seen.add
            return re.sub(regex, alt_seen, template)
    

    which returns the transformed template without duplicates and enforcing all similar blocks to have the same content.

    Afterwards it’s just a matter of replacing the blocks

    >>> unique_blocks_template = remove_duplicate_blocks(template)
    >>> print unique_blocks_template
    /data/${year}_${month}_${day}/(?P=year)(?P=month)(?P=day)_${type}_${id}.dat
    
    >>> string.Template(unique_blocks_template).safe_substitute(blocks)
    '/data/(?P<year>\\d{4})_(?P<month>\\d{2})_(?P<day>\\d{2})/(?P=year)(?P=month)(?P=day)_(?P<type>[BDPCLNIYSQJ])_(?P<id>\\d{8}).dat'
    

    Not mentioned in the question, but the same original template can also be used to reconstruct the string we want to match with the regex, which is the original aim of this code.

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

Sidebar

Related Questions

I want to build a regular expression which allows following cases: M1 1AA B33
I have an regular expression built in Java. String expr = <a.*?id=\(pres.*?)\.*?>Discharge.*?Medications:</a>; I want
I just want to create a regular expression out of any possible string. var
I'm trying to build a regular expression where I been asked to filter string
I want to build a console-like output using JTextPane. Therefore I am using a
I have a complex regular expression I've built with code. I want to normalize
I need to build ONE regular expression that can detect ALL the following strings
I have been trying to build a regular expression but haven't been able to
So, I want to built a regular expression that I can pass in a
I am trying to build a regular expression to replace unresolved velocity variables with

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.