Regex 匹配Tcl中除常规表达式模式之外的所有内容

Regex 匹配Tcl中除常规表达式模式之外的所有内容,regex,tcl,Regex,Tcl,我想匹配下面突出显示的行,这些行在函数定义之外。我试图编写一个正则表达式patternFunction.*?}来匹配完整的函数定义,但我想匹配函数定义之外的所有其他行 功能电路1{ I0 n1 n2 n3型 } I1 n1 n2 n3型 功能电路2{ I2 n1 n2 n3模型 } I3 n1 n2 n3 model由于要解析的数据看起来有点像Tcl,因此可以声明自己的DSL,将其解析为Tcl脚本 我将使用一个安全的解释器来隐藏任何潜在的恶意内容 set safe [interp create

我想匹配下面突出显示的行,这些行在函数定义之外。我试图编写一个正则表达式patternFunction.*?}来匹配完整的函数定义,但我想匹配函数定义之外的所有其他行

功能电路1{ I0 n1 n2 n3型 } I1 n1 n2 n3型 功能电路2{ I2 n1 n2 n3模型 }
I3 n1 n2 n3 model

由于要解析的数据看起来有点像Tcl,因此可以声明自己的DSL,将其解析为Tcl脚本

我将使用一个安全的解释器来隐藏任何潜在的恶意内容

set safe [interp create -safe]
interp eval $safe {
    # ignore Functions
    proc Function args {}
    # but keep everything else
    set wanted {}
    proc unknown args {lappend ::wanted $args}
}
interp invokehidden $safe source ./file
set lines_to_keep [interp eval $safe {set wanted}]
puts [join $lines_to_keep \n]

因为您想要解析的数据看起来有点像Tcl,所以您可以声明自己的DSL,将其解析为Tcl脚本

我将使用一个安全的解释器来隐藏任何潜在的恶意内容

set safe [interp create -safe]
interp eval $safe {
    # ignore Functions
    proc Function args {}
    # but keep everything else
    set wanted {}
    proc unknown args {lappend ::wanted $args}
}
interp invokehidden $safe source ./file
set lines_to_keep [interp eval $safe {set wanted}]
puts [join $lines_to_keep \n]

要保留除与regexp匹配的部分之外的所有内容,可以使用regsub将匹配部分替换为零:

regsub -all "Function.*?}" $input {}

要保留除与regexp匹配的部分之外的所有内容,可以使用regsub将匹配部分替换为零:

regsub -all "Function.*?}" $input {}

我不确定这会有多大帮助,但您可以忽略与此匹配的任何内容:

我尝试使用消极的前瞻,但由于量词的使用,没有取得太大的成功


这不是一个直接的答案,但可能是一个小的解决办法。

我不确定这会有多大帮助,但您可以忽略与此匹配的任何内容:

我尝试使用消极的前瞻,但由于量词的使用,没有取得太大的成功


这不是一个直接的答案,但可能是一个小的解决办法。

正则表达式不是用来计算括号的。在理论上,匹配自动机的复杂性是不同的。但是Tcl确实有一个内置的括号和括号匹配器,info complete,只要您愿意接受一些语法约束。它们通常只适用于模糊的编程输入,而且在您的示例输入中也绝对适用

set accumulate ""
foreach line [split $inputData "\n"] {
    if {$accumulate ne ""} {
        # Add in the line *with newline separator*
        append accumulate \n $line
        # If it's brace-balanced...
        if {[info complete $accumulate]} {
            # ... drop the accumulated lines; we're not interested in them from here on
            set accumulate ""
        }
    } elseif {[regexp {^\s*Function\s+\w+\s+\{\s*$} $line]} {
        # Start to collect lines to skip
        set accumulate $line
    } else {
        # Found what we're looking for
        puts "This is a line of interest: $line"
    }
}

请注意,我使用了正则表达式来确定何时开始匹配,大括号匹配模式处理在这之前,这样任何嵌套函数都不会出错。

正则表达式的设计目的不是进行大括号计数。所需匹配自动机的复杂性在理论上存在差异。但是Tcl确实有一个内置的括号和括号匹配器,info complete,只要您愿意接受一些语法约束。它们通常只适用于模糊的编程输入,而且在您的示例输入中也绝对适用

set accumulate ""
foreach line [split $inputData "\n"] {
    if {$accumulate ne ""} {
        # Add in the line *with newline separator*
        append accumulate \n $line
        # If it's brace-balanced...
        if {[info complete $accumulate]} {
            # ... drop the accumulated lines; we're not interested in them from here on
            set accumulate ""
        }
    } elseif {[regexp {^\s*Function\s+\w+\s+\{\s*$} $line]} {
        # Start to collect lines to skip
        set accumulate $line
    } else {
        # Found what we're looking for
        puts "This is a line of interest: $line"
    }
}

注意,我使用了一个正则表达式来确定何时开始匹配,大括号匹配模式处理在此之前,这样任何嵌套函数都不会出错。

查找匹配大括号不是正则表达式特别擅长的事情。查找匹配大括号不是正则表达式特别擅长的事情。数据不是TCL脚本,看起来是这样,但不是这样。@Akhil它必须看起来足够像tcl才能被源代码解析。不需要是实际的tcl。数据不是tcl脚本,它看起来像tcl脚本,但不是。@Akhil它只需要看起来足够像tcl就可以被源解析。不需要是真正的tcl。