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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:22:37+00:00 2026-05-20T05:22:37+00:00

Let’s say I have the following multi-line string: # Section ## Subsection ## Subsection

  • 0

Let’s say I have the following multi-line string:

# Section
## Subsection
## Subsection
# Section
## Subsection
### Subsubsection
### Subsubsection
# Section
## Subsection

and I want it to become:

# 1 Section
## 1.1 Subsection
## 1.2 Subsection
# 2 Section
## 2.1 Subsection
### 2.1.1 Subsubsection
### 2.1.2 Subsubsection
# 3 Section
## 3.1 Subsection

In Python, using the re module, is it be possible to run a substitution on the string which would:

  • Match the beginning of each line based on the number of #‘s
  • Keep track of past matches of commonly-numbered groups of #‘s
  • Insert this counter when appropriate into the line

…assuming that any of these ‘counters’ are always non-zero?

This problem is testing the limits of my regex knowledge. I already know I can just iterate over the lines and increment/insert some variables, so that’s not the solution I want. I’m simply curious if this kind of functionality exists solely within a regular expressions, as I know that some sort of counting already exists (e.g., number of substitutions to make).

  • 1 1 Answer
  • 1 View
  • 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-20T05:22:37+00:00Added an answer on May 20, 2026 at 5:22 am

    « Ok, sure, but what if the ‘variable manipulation’ is being done in a callback function of re.sub, can it be done then? I guess a simplified form of my question is: “Can one use regular expressions to substitue differently based on previous matches?” »

    It sounds like we need a generator function as a callback; unfortunately, re.sub() doesn’t accept a generator function as a callback.

    So we must use some trick:

    import re
    
    pat = re.compile('^(#+)',re.MULTILINE)
    
    ch = '''# Section
    ## Subsection
    ## Subsection
    # Section
    ## Subsection
    ### Subsubsection
    ### Subsubsection
    ## Subsection
    ### Subsubsection
    ### Subsubsection
    #### Sub4section
    #### Sub4section
    #### Sub4section
    #### Sub4section
    ##### Sub5section
    #### Sub4section
    ##### Sub5section
    ##### Sub5section
    ### Subsubsection
    ### Subsubsection
    #### Sub4section
    #### Sub4section
    ## Subsection
    ### Subsubsection
    ### Subsubsection
    ### Subsubsection
    #### Sub4section
    ##### Sub5section
    ##### Sub5section
    ### Subsubsection
    #### Sub4section
    ## Subsection
    ### Subsubsection
    ### Subsubsection
    # Section
    ## Subsection
    ## Subsection
    # Section
    ## Subsection
    ### Subsubsection
    #### Sub4section
    #### Sub4section
    #### Sub4section
    ##### Sub5section
    #### Sub4section
    ### Subsubsection
    ## Subsection
    ### Subsubsection
    # Section
    ## Subsection
    '''
    
    def cbk(match, nb = [0] ):
        if len(match.group())==len(nb):
            nb[-1] += 1
        elif  len(match.group())>len(nb):
            nb.append(1)
        else:
            nb[:] = nb[0:len(match.group())]
            nb[-1] += 1
        return match.group()+' '+('.'.join(map(str,nb)))
    
    ch = pat.sub(cbk,ch)
    print ch
    

    .

    « Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. »

    http://docs.python.org/reference/compound_stmts.html#function

    But here, it IS my plain intent.

    Result:

    # 1 Section
    ## 1.1 Subsection
    ## 1.2 Subsection
    # 2 Section
    ## 2.1 Subsection
    ### 2.1.1 Subsubsection
    ### 2.1.2 Subsubsection
    ## 2.2 Subsection
    ### 2.2.1 Subsubsection
    ### 2.2.2 Subsubsection
    #### 2.2.2.1 Sub4section
    #### 2.2.2.2 Sub4section
    #### 2.2.2.3 Sub4section
    #### 2.2.2.4 Sub4section
    ##### 2.2.2.4.1 Sub5section
    #### 2.2.2.5 Sub4section
    ##### 2.2.2.5.1 Sub5section
    ##### 2.2.2.5.2 Sub5section
    ### 2.2.3 Subsubsection
    ### 2.2.4 Subsubsection
    #### 2.2.4.1 Sub4section
    #### 2.2.4.2 Sub4section
    ## 2.3 Subsection
    ### 2.3.1 Subsubsection
    ### 2.3.2 Subsubsection
    ### 2.3.3 Subsubsection
    #### 2.3.3.1 Sub4section
    ##### 2.3.3.1.1 Sub5section
    ##### 2.3.3.1.2 Sub5section
    ### 2.3.4 Subsubsection
    #### 2.3.4.1 Sub4section
    ## 2.4 Subsection
    ### 2.4.1 Subsubsection
    ### 2.4.2 Subsubsection
    # 3 Section
    ## 3.1 Subsection
    ## 3.2 Subsection
    # 4 Section
    ## 4.1 Subsection
    ### 4.1.1 Subsubsection
    #### 4.1.1.1 Sub4section
    #### 4.1.1.2 Sub4section
    #### 4.1.1.3 Sub4section
    ##### 4.1.1.3.1 Sub5section
    #### 4.1.1.4 Sub4section
    ### 4.1.2 Subsubsection
    ## 4.2 Subsection
    ### 4.2.1 Subsubsection
    # 5 Section
    ## 5.1 Subsection
    

    EDIT 1 : I corrected else nb[:] = nb[0:len(match.group())] to else: only

    EDIT 2 : the code can be condensed to

    def cbk(match, nb = [0] ):
        if len(match.group())>len(nb):
            nb.append(1)
        else:
            nb[:] = nb[0:len(match.group())]
            nb[-1] += 1
        return match.group()+' '+('.'.join(map(str,nb))) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have an Instant Messenger server using SignalR. I want to broadcast
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have thousands of users and I want to make the passwords
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have the string: hello world; some random text; foo; How could

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.