VB.NET替换变量找到的文件中的代码块

时间:2016-01-02 15:21:42

标签: regex vb.net

假设我在本地Github中有文件,我需要替换此代码

    bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)
{
    if (!fReadTransactions)
    {
        *this = pindex->GetBlockHeader();
        return true;
    }
    if (!ReadFromDisk(pindex->nFile, pindex->nBlockPos, fReadTransactions))
        return false;
    if (GetHash() != pindex->GetBlockHash())
        return error("CBlock::ReadFromDisk() : GetHash() doesn't match index");
    return true;
}

我正在寻找一种“扫描”文件并匹配初始

的方法
bool CBlock::ReadFromDisk(const CBlockIndex* pindex, bool fReadTransactions)

并完全替换括号{}之间的代码。

因此,我所知道的关于我需要替换的块的唯一部分是声明。

1 个答案:

答案 0 :(得分:1)

要匹配(和)之间的所有内容,您可以使用:

(?<=bool CBlock::ReadFromDisk\().*?(?=\))
Dim ResultString As String
Try
    ResultString = Regex.Replace(SubjectString, "(?<=bool CBlock::ReadFromDisk\().*?(?=\))", "replacement_text_here", RegexOptions.Multiline)
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try

要匹配{和}之间的所有内容,您可以使用:

(?<=bool CBlock::ReadFromDisk\(const CBlockIndex\* pindex, bool fReadTransactions\)\s*\{)(?:[^{}]|(?<open>{)|(?<-open>}))+(?(open)(?!))(?=\})
Dim ResultString As String
Try
    ResultString = Regex.Replace(SubjectString, "(?<=bool CBlock::ReadFromDisk\(const CBlockIndex\* pindex, bool fReadTransactions\)\s*\{)(?:[^{}]|(?<open>{)|(?<-open>}))+(?(open)(?!))(?=\})", "replacement_text_here")
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try