简体   繁体   English

用正则表达式替换多行

[英]Replace multiple lines with regex

Given the following text, I want to remove everything in data_augmentation_options{..}鉴于以下文本,我想删除data_augmentation_options{..}中的所有内容

ie, input is:即,输入是:

  batch_size: 4
  num_steps: 30
  data_augmentation_options {
    random_horizontal_flip {
      keypoint_flip_permutation: 0
      keypoint_flip_permutation: 2
      keypoint_flip_permutation: 1
      keypoint_flip_permutation: 4
      keypoint_flip_permutation: 3
      keypoint_flip_permutation: 6
      keypoint_flip_permutation: 5
      keypoint_flip_permutation: 8
      keypoint_flip_permutation: 7
      keypoint_flip_permutation: 10
      keypoint_flip_permutation: 9
      keypoint_flip_permutation: 12
      keypoint_flip_permutation: 11
      keypoint_flip_permutation: 14
      keypoint_flip_permutation: 13
      keypoint_flip_permutation: 16
      keypoint_flip_permutation: 15
    }
  }

  data_augmentation_options {
    random_crop_image {
      min_aspect_ratio: 0.5
      max_aspect_ratio: 1.7
      random_coef: 0.25
    }
  }

expected output is:预计 output 是:

  batch_size: 4
  num_steps: 30  

I tried我试过了

s='''
      batch_size: 4
      num_steps: 30
      data_augmentation_options {
        random_horizontal_flip {
          keypoint_flip_permutation: 0
          keypoint_flip_permutation: 2
          keypoint_flip_permutation: 1
          keypoint_flip_permutation: 4
          keypoint_flip_permutation: 3
          keypoint_flip_permutation: 6
          keypoint_flip_permutation: 5
          keypoint_flip_permutation: 8
          keypoint_flip_permutation: 7
          keypoint_flip_permutation: 10
          keypoint_flip_permutation: 9
          keypoint_flip_permutation: 12
          keypoint_flip_permutation: 11
          keypoint_flip_permutation: 14
          keypoint_flip_permutation: 13
          keypoint_flip_permutation: 16
          keypoint_flip_permutation: 15
        }
      }
    
      data_augmentation_options {
        random_crop_image {
          min_aspect_ratio: 0.5
          max_aspect_ratio: 1.7
          random_coef: 0.25
        }
      }
'''
print(re.sub('data_augmentation_options \{*\}','',s,flags=re.S))

It does not seem to work, what's the right way to achieve this?它似乎不起作用,实现这一目标的正确方法是什么?

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.如果你想使用re.sub ,你可以使用一个冲突的字符 class ,它将匹配匹配后的任何和所有字符。 A conflicted character class is something like [\s\S] which is a space or non-space character:冲突字符 class 类似于[\s\S] ,它是空格或非空格字符:

>>> 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:也许更简单的方法是将 Python 的str.partition与要用作分隔符的字符串一起使用:

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

        batch_size: 4
        num_steps: 30

This will work:这将起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM