Python只替换不是C ++注释的字符串

时间:2015-07-13 07:26:44

标签: python regex replace

我只想处理 C++评论的字符串,这是找出C++评论的模式:

pattern = re.compile(r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', re.DOTALL | re.MULTILINE)

但是,我不知道如何让它像我的意图一样工作。

# Python 3.4.2
s = '''
/****
C++ comments
  //pResMgr->CreateDialogEx();
****/
//pResMgr->CreateDialogEx();
/*//pResMgr->CreateDialogEx();*/

// real code, I want to replace only this following line of code
pResMgr->CreateDialogEx();
'''

newS = s.replace('CreateDialogEx', 'Create')
print(newS)

我的预期输出是:

/****
C++ comments
  //pResMgr->CreateDialogEx();
****/
//pResMgr->CreateDialogEx();
/*//pResMgr->CreateDialogEx();*/

// real code, I want to replace only this following line of code
pResMgr->Create();

1 个答案:

答案 0 :(得分:2)

没有测试它,但它适用于你的情况,从根本上应该工作。它基本上是通过文本查找换行符,//或/ *然后处理案例。真的很简单,没有正则表达式。

source_code = '''//pResMgr//->CreateDialogEx();'''

def indexOf(string, character):
    return string.index(character) if character in string else 9999999

def replaceNotInComments(string, searchFor, replaceWith):
    result = ''
    nextBreak = 0
    while True:
        nextBreak = min(indexOf(string, '\n'),
                        indexOf(string, '/*'),
                        indexOf(string, '//'))
        if nextBreak == 9999999:
            result += string.replace(searchFor, replaceWith);
            break
        result += string[0:nextBreak].replace(searchFor, replaceWith);

        if nextBreak == indexOf(string, '\n'):
            string = string[nextBreak+1:]

        if nextBreak == indexOf(string, '/*'):
            string = string[nextBreak+2:]
            result += '/*'+string[0:indexOf(string, '*/')+2]
            string = string[indexOf(string, '*/')+2:]

        if nextBreak == indexOf(string, '//'):
            string = string[nextBreak+2:]
            if result != '':
                result += '\n'  
            result += string[0:indexOf(string, '\n')+1]
            string = string[indexOf(string, '\n')+1:]


    return result

result = replaceNotInComments(source_code, 'CreateDialogEx', 'Create')
print(result)