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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:59:48+00:00 2026-05-31T03:59:48+00:00

I have a special use case which I do not yet know how to

  • 0

I have a special use case which I do not yet know how to cover. I want to dissect a string based on field_name/field_length. For that I define a regex like this:

'(?P<%s>.{%d})' % (field_name, field_length)

And this is repeated for all fields.

I have also a regex to remove spaces to the right of each field:

self.re_remove_spaces = re.compile(' *$')

This way I can get each field like this:

def dissect(self, str):
    data = { }
    m = self.compiled.search(str)
    for field_name in self.fields:
        value = m.group_name(field_name)
        value = re.sub(self.re_remove_spaces, '', value)
        data[field_name] = value
    return data

I have to perform this processing for millions of strings, so it must be efficient.

What annoys me is that I would prefer to perform the dissection + space removal in a single step, using compiled.sub instead of compiled.search, but I do not know how to do this.

Specifically, my question is:

How do I perform regex substitution combining it with named groups in Python regexes?

  • 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-31T03:59:50+00:00Added an answer on May 31, 2026 at 3:59 am

    I take it each field sits next to each other in the string, like in a table, e.g.:

    name     description        license
    python   language           opensource
    windows  operating system   proprietry
    

    So assuming you know in advance the length of each field, you can do it much more simply, without using a regex at all. (btw, str is not a good name for a variable since it clashes with the builtin str type)

    def dissect(text):
        data = {}
        for name, length in fields:
            data[name] = text[:length].rstrip()
            text = text[length:]
        return data
    

    Then, if fields = [('lang', 9), ('desc', 19), ('license', 12)]:

    >>> self.dissect('python   language           opensource')
    {'lang': 'python', 'license': 'opensource', 'desc': 'language'}
    

    Is this what you’re trying do though?

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

Sidebar

Related Questions

I use special characters (swedish letters åäö). Now, I have some folders, which contains
I have a special url which I would want only few people to have
Applications which run on mobile devices have special user interface requirements. I think Apple
I have several special methods, and I want analyze they calls in compiled assembly.
I have a call to a model which is just a standard class (not
I have a query in a php string which i am trying to filter
The following code, which attempts to specialize class template 'special', based on the return
I have a string with special characters insert in different places. For example: string
I use TeamCity which in turn invokes msbuild (.NET 4). I have a strange
Is it possible to have special characters like åäö in the key? If i

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.