首页 > 解决方案 > 如何删除 C 文件中的多行注释以使用 Python?

问题描述

我正在使用 .readlines() 读取 C 文件,以获取 C 代码文件的所有单独行作为列表的元素。如果任何列表元素(即 C 代码行)是多行注释的一部分,则需要将其替换为 ''(即删除它但它将占用列表的相应索引),以便不更改其他行的索引. 请检查我的代码以获取详细信息。

import re

#open and read sample.c so that code lines of sample.c are the element of list 'flist'

with open('sample.c',mode='r') as myfile: 
    flist = myfile.readlines() 


pattern1 = re.compile(r'\/{2}.*')         # to match //
pattern2 = re.compile('\/\*.*\*\/')       # to match /*......*/ in a single line or element of flist
pattern3 = re.compile('\/\*')             # to match /*
pattern4 = re.compile('\*\/')             # to match */ 

# to delete comments present in a single code line or can say in an individual element of 'flist', it's working fine.

for index,line in enumerate(flist):
    if pattern2.search(line):
       flist[index] = pattern2.sub("",line)
    if pattern1.search(line):
        flist[index] = pattern1.sub("",line)

# to delete comment starting in one line and ending in the subsequent line, **this following part is not working** 

cmnt_e = True
cmnt_s = False

for index,line in enumerate(flist):
    if pattern3.search(line) and cmnt_e == True:
        cmnt_s = True
        cmnt_e = False
    if pattern3.search(line) and cmnt_s == True:
        cmnt_e = True
        cmnt_s = False
    if cmnt_s == True and cmnt_e == False:
        flist[index] = ''

标签: pythonregexautomation

解决方案


我并没有看太多细节,但是您正在使用 pattern3 来开始和结束评论。我想它应该是这样的

    if pattern3.search(line) and cmnt_e == True:
        cmnt_s = True
        cmnt_e = False
    if pattern4.search(line) and cmnt_s == True:
        cmnt_e = True
        cmnt_s = False

我也会警惕像这样的奇怪模式(是的,我有时将它用于实验性的东西,你只需翻转第一个斜线来翻转活动代码):

//*
ACTIVE_CODE
/*/
INACTIVE_CODE
//*/

推荐阅读