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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:21:40+00:00 2026-06-12T06:21:40+00:00

I am just recently learning and utilizing the power of regular expressions I have

  • 0

I am just recently learning and utilizing the power of regular expressions
I have a tuple list of files returned from os.walk(), like so:

files = ('s8_00.tif', 's9_00.tif', 's10_000.tif', 's11_00.tif')

I am trying to get it to look like this:

files = ('s8_##.tif', 's9_##.tif', 's10_###.tif', 's11_##.tif')

I have tried to use this.

pad2 = re.compile(r'_00?')

for root, dirs, files in seqDirs:
  pad = files[0]  
  p = pad2.sub("#", pad)
  print p

This returns:

p = ('s8#.tif', 's9#.tif', 's10#0.tif', 's11#.tif')

So I changed the expression around to:

pad2 = re.compile('(_)0+')

giving me:

p = ('s8#.tif', 's9#.tif', 's10#.tif', 's11#.tif')

Is the problem in my p = pad2.sub function? Or is the problem exist within my compiled expression? Or is it the "_" being in the expression that is screwing it up?

I tried even passing some expression inside the pad2.sub function just to test it out and of course that didn’t really work. I know I am missing something little here and I am a bit stuck.

Any and all help will be greatly appreciated along with explanations of logic.

  • 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-12T06:21:42+00:00Added an answer on June 12, 2026 at 6:21 am

    We’re going to use a function for the replacement, not a string.

    def replacer(data):
        return re.sub(r'(?<=_)(0+)', lambda m: m.group(0).replace('0', '#'), data)
    
    files = ('s8_000.tif', 's9_00.tif', 's10_000.tif', 's11_00.tif')
    map(replacer, files)
    print(files)
    

    ?<= is a positive lookbehind assertion. You can find an explanation in the docs at Regular Expression Syntax.

    0+ captures all following zeros

    The lambda function replaces every 0 with the #.

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

Sidebar

Related Questions

I have just recently started learning Haskell and I am having a lot of
I have recently just started learning how to develop android applications. I am am
I just recently started learning Java. I have a question which is more about
I come from a C# background and have recently started learning C++. One of
I just recently started learning the Scala language and would like to do it
I just recently have started learning about the wonders of **kwargs but I've hit
I've recently been learning the MVVM pattern in WPF and just started making my
Warning: I just started learning bash recently and trying to do a recursive function
So just recently, my async server code which relies on pthread_mutex_timedlock seem to have
I just recently started learning RoR and found that Prototype.js is a default javascript

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.