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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:54:12+00:00 2026-06-12T23:54:12+00:00

I am trying to replace variable length items in a list using regex. For

  • 0

I am trying to replace variable length items in a list using regex. For example this item “HD479659” should be replaced by “HD0000000479659″. I need just to insert 7 0s in between.I have made the following program but every time I run it I got the following error:”TypeError: object of type ‘_sre.SRE_Pattern’ has no len()”. Can you please help me how to solve this error.

thank you very much

Here is the program

import xlrd  
import re
import string

wb = xlrd.open_workbook("3_1.xls") 

sh = wb.sheet_by_index(0) 

outfile=open('out.txt','w')


s_pat=r"HD[1-9]{1}[0-9]{5}"

s_pat1=r"HD[0]{7}[0-9]{6}"

pat = re.compile(s_pat) 

pat1 = re.compile(s_pat1)



for rownum1 in range(sh.nrows): 

  str1= str(sh.row_values(rownum1))

  m1=[]

  m1 = pat.findall(str1)

  m1=list(set(m1))

  for a in m1:

      a=re.sub(pat,pat1,a)

  print >> outfile, m1
  • 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-12T23:54:13+00:00Added an answer on June 12, 2026 at 11:54 pm

    I think your solution is quite to complicated. This one should do the job and is much simpler:

    import re
    
    def repl(match):
        return match.group(1) + ("0"*7) + match.group(2)
    
    print re.sub(r"(HD)([1-9]{1}[0-9]{5})", repl, "HD479659")
    

    See also: http://docs.python.org/library/re.html#re.sub

    Update:

    To transform a list of values, you have to iterate over all values. You don’t have to search the matching values first:

    import re
    
    values_to_transform = [
        'HD479659',
        'HD477899',
        'HD423455',
        'does not match',
        'but does not matter'
    ]
    
    def repl(match):
        return match.group(1) + ("0"*7) + match.group(2)
    
    for value in values_to_transform:
        print re.sub(r"(HD)([1-9]{1}[0-9]{5})", repl, value)
    

    The result is:

    HD0000000479659
    HD0000000477899
    HD0000000423455
    does not match
    but does not matter
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace a variable stored in another file using regular expression.
I'm trying to replace all the occurrences of a variable in a string using
I am trying to replace a value with a passed value using javascript or
I'm trying to replace the value of an environment variable definition, if it exists,
i am trying to replace spaces with underscore '_' in the following variable $e_type
I am trying to replace multiple string values at once from one variable and
I cant get this to work. I am trying to replace certain characters when
I'm trying to do a URL GET variable replace, however the regular expression for
Basically I'm trying to replace ? by pressing any keypad button. For example, if
I'm trying to pass a JavaScript variable to the server-side using jquery.ajax method. I'm

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.