首页 > 编程语言 >智能合约Solidity去除空行与所有注释代码Python脚本

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

时间:2023-02-22 22:15:21浏览次数:42  
标签:空行 group Python res 代码 Solidity re line match

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

标签:空行,group,Python,res,代码,Solidity,re,line,match
From: https://www.cnblogs.com/Alei777/p/17146123.html

相关文章