Skip to content Skip to sidebar Skip to footer

Replace Multiple Lines With Regex

Given the following text, I want to remove everything in data_augmentation_options{..} i.e., input is : batch_size: 4 num_steps: 30 data_augmentation_options { random_hor

Solution 1:

Rather than deleting what you don't want you could capture what you do want:

>>> re.findall(r'batch_size: *\d+|num_steps: *\d+',s)
['batch_size: 4', 'num_steps: 30']

Or if you want to capture the leading spaces:

>>> re.findall(r'^[ \t]*(?:batch_size:|num_steps:)[ \t]*\d+',s, flags=re.M)
['\t\t\tbatch_size: 4', '\t\t\tnum_steps: 30']

Then print the result:

>>>print('\n'.join(re.findall(r'^[ \t]*(?:batch_size:|num_steps:)[ \t]*\d+',s, flags=re.M))
        batch_size: 4
        num_steps: 30

If you want to use re.sub you can use a conflicted character class that will match any and all characters after the match. A conflicted character class is something like [\s\S] which is a space or non-space character:

>>>re.sub(r'data_augmentation_options[\s\S]*','',s)

        batch_size: 4
        num_steps: 30

Perhaps even easier is to just use Python's str.partition with the string that you want to use as a separator:

>>>s.partition('data_augmentation_options')[0]

        batch_size: 4
        num_steps: 30

Solution 2:

This will work:

s = re.sub("\W+data_augmentation_options {(?:.|\n)*}", "", s).strip()

Post a Comment for "Replace Multiple Lines With Regex"