智能合约Solidity去除空行与所有注释代码Python脚本

import re


def removeComments(string):
    pattern = r"(\".*?\"|\'.*?\')|((?s)/\*.*?\*/)|(//[^\r\n]*$)"
    regex = re.compile(pattern, re.MULTILINE | re.DOTALL)

    def _replacer(match):
        if match.group(2) is not None:
            return " "
        else:
            return match.group(1) or match.group(3)

    return regex.sub(_replacer, string)

with open("0.txt", "r", encoding="utf-8") as f:
    line = f.readline();
    while line:
        line = line.strip()  # 去掉换行符"\n
        res = removeComments(line)
        if(res.isspace()==False and len(res)!=0):
            print(res)
        line=f.readline()

1、0.txt可以替换为你需要替换的智能合约源代码文件。

2、我是一句代码一句代码的处理,将代码通过正则表达式是否满足三种注释的原则。其次,判断该句话是不是为空行。最终进行输出答案。

posted @ 2023-02-22 21:54  雷雷提  阅读(44)  评论(0编辑  收藏  举报