Search code examples
pythonregexsubstitutionpython-re

Python regex substitute syntax skip lines with certain criteria


I would like to make substitutions within a string only if the line meets certain criteria.

text_old = """
with some text 
some text as(
-- from text some text
with select text some other text
-- with text
from text
"""

where my substitutions are -

replacements = [
    ('with ','with \n'),
    ('as\n ','as'), 
    ('as\(','as (') 
        ]
for old, new in replacements:
    text_new = re.sub(old,new,text_old,flags=re.IGNORECASE)

And I want to skip the substitution if the line starts with -- . So the from and with replacements are skipped here -

-- from text some text
-- with text

Solution

  • You can solve this problem with pure regex with the PyPi regex module. Go to console/terminal and run the pip install regex command. This will allow you to import regex in your script and all that remains to do is add (?<!^--.*) to each regex:

    replacements = [
        (r'(?<!^--.*)\bwith ','with \n'),
        (r'(?<!^--.*)\bas\n ','as'), 
        (r'(?<!^--.*)\bas\(','as (') 
    ]
    

    You also need to use re.M (regex.M) flag to make sure ^ matches all line start positions and not just a start of the whole string. See the Python demo:

    import regex as re
    
    text_old = """
    with some text 
    some text as(
    -- from text some text
    with select text some other text
    -- with text
    from text
    """
    
    replacements = [
        (r'(?<!^--.*)\bwith ','with \n'),
        (r'(?<!^--.*)\bas\n ','as'), 
        (r'(?<!^--.*)\bas\(','as (') 
    ]
    text_new = text_old
    for old, new in replacements:
        text_new = re.sub(old,new,text_new,flags=re.I|re.M)
    
    print(text_new)
    

    Output:

    
    with 
    some text 
    some text as (
    -- from text some text
    with 
    select text some other text
    -- with text
    from text