Search code examples
pythonpython-3.xstringpython-re

Replace characters before a number to a new character after the number python


I have some strings look like: *.rem.1.gz and *.rem.2.gz

And I want to replace it into *.1.trim.gz and *.2.trim.gz

The number 1 and number two files are paired with each other, which I want to create a separate string to include both files at once.

import os
allfiles = os.listdir
pair = [x.replace("rem.(\d+)","(\d+).trim")for x in allfiles] ##obviously this doesn't work but just want to give an idea what I want to do!

Thanks for the help!


Solution

  • You can use (?i)(.*)\.rem\.([0-9]+)\.gz if it is case insensitive or (.*)\.rem\.([0-9]+)\.gz otherwise.

    Example

    import re
    
    s = "*.rem.1.gz"
    p = r'(?i)(.*)\.rem\.([0-9]+)\.gz'
    print(re.sub(p, r'\1.\2.trim.gz', s))
    
    

    Prints

    *.1.trim.gz
    

    Using the idea in the following comment, you can simplify the code.

    There's no need to capture the base name in a group. Anchor the end of the string with $ instead. – blhsing

    Example

    import re
    
    s = "*.rem.1.gz"
    p = r'(?i)\.rem\.([0-9]+)\.gz$'
    print(re.sub(p, r'.\1.trim.gz', s))