登录
首页 >  Golang >  Go问答

解决Golang多行正则表达式解析的挑战

来源:stackoverflow

时间:2024-03-26 10:09:45 229浏览 收藏

在解析 Solidity 代码时,多行正则表达式匹配是一个挑战。本文介绍了一种解决此问题的解决方案,通过将源代码转换为字符串,然后使用 (?s) 分组和 \n 分割匹配内容,从而实现对多行代码的匹配。通过修改代码,成功匹配了 require() 之间的内容,并输出解析结果。

问题内容

我正在用 go 创建一个解析 solidity 代码的项目。在我的项目中,我创建了一个函数 analyzefile(),它对于每个智能合约 (.sol) 都会检测正则表达式的静态问题:

func analyzefile(issues []issue, file string) (map[string][]finding, error) {
    findings := make(map[string][]finding)
    readfile, err := os.open(file)
    if err != nil {
        return nil, err
    }
    defer readfile.close()
    contents, _ := ioutil.readfile(file)
    scanner := bufio.newscanner(readfile)
    linenumber := 0
    for scanner.scan() {
        linenumber++
        line := scanner.text()
        for _, issue := range issues {
            if issue.parsingmode == "singleline" {
                matched, _ := regexp.matchstring(issue.pattern, line)
                if matched {
                    findings[issue.identifier] = append(findings[issue.identifier], finding{
                        issueidentifier: issue.identifier,
                        file:            file,
                        linenumber:      linenumber,
                        linecontent:     strings.trimspace(line),
                    })
                }
            }
        }
    }

当正则表达式必须控制单行代码时,一切都很好。 但是,我还需要检查 .sol 文件中多行出现的内容,例如检测这段代码:

require(
  _disputeid < disputecount &&
  disputes[_disputeid].status == status.active,
  "disputes::!resolvable"
);

我尝试在analyzefile()函数中添加以下代码:

 contents, _ := ioutil.ReadFile(file)
    for _, issue := range issues {
        if issue.ParsingMode == "MultiLine" {
            contents_to_string := string(contents)
            //s := strings.ReplaceAll(contents_to_string, "\n", " ")
            //sr := strings.ReplaceAll(s, "\r", " ")
            r := regexp.MustCompile(`((require)([(])\n.*[&&](?s)(.*?)([;]))`)
            finds := r.FindStringSubmatch(contents_to_string)
            for _, find := range finds {
                findings[issue.Identifier] = append(findings[issue.Identifier], Finding{
                    IssueIdentifier: issue.Identifier,
                    File:            file,
                    LineContent:     (find),
                })
            }
        }
    }

但是我得到了错误的结果,因为当将源代码转换为字符串时,我将所有代码放在一行上,并带有换行符 \n 字符,这使得任何正则表达式检查崩溃。


正确答案


解决方案中的一个单词可以在 caputer 组 (?s)require\((.*?)\); 之后用 \n 分割整个字符串。

func main() {
    var re = regexp.mustcompile(`(?s)require\((.*?)\);`)
    var str = `require(
  _disputeid < disputecount &&
  disputes[_disputeid].status == status.active,
  "disputes::!resolvable"
);`

    matches := re.findallstringsubmatch(str, -1)
    for _, match := range matches {
        lines := strings.split(match[1], "\n")
        for _, line := range lines {
            fmt.println(line)
        }
    }
}

https://go.dev/play/p/Omn5ULHun_-

为了匹配多行,可以使用 (?m)^[^\s\r\n]*(.*)[^\s\r\n](\s+)$。我们可以对 require() 之间的内容进行多行匹配

func main() {
    var re = regexp.mustcompile(`(?s)require\((.*?)\);`)
    var str = `require(
  _disputeid < disputecount &&
  disputes[_disputeid].status == status.active,
  "disputes::!resolvable"
);`

    var multilinere = regexp.mustcompile(`(?m)^[^\s\r\n]*(.*)[^\s\r\n](\s+)$`)
    matches := re.findallstringsubmatch(str, -1)
    for _, match := range matches {
        submathes := multilinere.findallstringsubmatch(match[1], -1)
        for _, submatch := range submathes {
            fmt.println(submatch[0])
        }
    }
}

https://go.dev/play/p/LJsVy5vN6Ej

通过修改代码,我设法让它工作:

contents, _ := ioutil.readfile(file)
    for _, issue := range issues {
        if issue.parsingmode == "multilineg015" {
            str := string(contents)
            var re = regexp.mustcompile(`(?s)require\((.*?)\);`)
            //var multilinere = regexp.mustcompile(`(?m)^[^\s\r\n]*(.*)[^\s\r\n](\s+)$`)
            //getting all require in the sol file
            matches := re.findallstringsubmatch(str, -1)
            r := regexp.mustcompile(issue.pattern)
            for _, match := range matches {
                submatches := r.findallstringsubmatch(match[0], -1)
                for _, submatch := range submatches {
                    findings[issue.identifier] = append(findings[issue.identifier], finding{
                        issueidentifier: issue.identifier,
                        file:            file,
                        linecontent:     ([]string{submatch[0]}),
                    })
                }
            }

这是输出:

2022-08-rigor\contracts\Community.sol::0 => [(
            _lendingNeeded >= _communityProject.totalLent &&
                _lendingNeeded <= IProject(_project).projectCost(),
            "Community::invalid lending"
        );]
2022-08-rigor\contracts\Disputes.sol::0 => [(
            _disputeID < disputeCount &&
                disputes[_disputeID].status == Status.Active,
            "Disputes::!Resolvable"
        );]
2022-08-rigor\contracts\Disputes.sol::0 => [(
            _actionType > 0 && _actionType <= uint8(ActionType.TaskPay),
            "Disputes::!ActionType"
        );]
2022-08-rigor\contracts\Project.sol::0 => [(
            _sender == builder || _sender == homeFi.communityContract(),
            "Project::!Builder&&!Community"
        );]

感谢zangw的帮助!

理论要掌握,实操不能落!以上关于《解决Golang多行正则表达式解析的挑战》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>