简体   繁体   English

如何通过 PYTHON 删除特定字符串前上一行的逗号

[英]How to delete the commas at the previous line before specific string by PYTHON

I'm dealing with a tough question.我正在处理一个棘手的问题。 I need to delete some commas at the end of the line, which is previous some specific strings.我需要在行尾删除一些逗号,这是之前的一些特定字符串。

Such as:如:

define{
   varA,
   varB,
   varC
}

The specific string is varC, and I want to delete varC and the comma(,) after varB at the same time.具体字符串是varC,我想同时删除varC和varB后面的逗号(,)。

The modified text is修改后的文字是

define{
   varA,
   varB
}

I must deal with many code files so I need a script to do it, but that's tough for me.我必须处理许多代码文件,所以我需要一个脚本来完成,但这对我来说很难。

You could use a regex that looks for the define blocks, separating them in 3 groups:您可以使用查找define块的正则表达式,将它们分成 3 组:

  • the first one starting with define{... and taking everything afterwards non-greedily, including newlines (so we'll need the re.DOTALL flag to allow . to match newlines)第一个以define{...开头,然后以非贪婪方式获取所有内容,包括换行符(因此我们需要re.DOTALL标志以允许.匹配换行符)
  • the second one is the part we want to remove: a comma, some space, some word第二个是我们要删除的部分:逗号、空格、单词
  • the third one is the final spaces and closing }第三个是最后的空格和关闭}

We just have to use re.sub to replace the matches by the first and third groups only:我们只需要使用re.sub来替换第一组和第三组的匹配项:

data = """ 
some code
some more code
define{
   varA,
   varB,
   varC
}
some code
define{
   varD,
   varE
}
end of code
"""

import re

define_re = re.compile(r'(define{.*?)(,\s+\w+)(\s+})', re.DOTALL)
out = define_re.sub(r'\1\3', data)

print(out)

Output: Output:

some code
some more code
define{
   varA,
   varB
}
some code
define{
   varD
}
end of code

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

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