【问题标题】:Regular Expression within tcltcl 中的正则表达式
【发布时间】:2015-12-15 20:57:11
【问题描述】:

tcl 我给这个是:-

set promptInfo [oBOTANDROID2 launchVapiEiCmd getinfo]

并获得输出为“-

VAPIEI Cmd Rcvd: {state inservice} {type dmc} {description {BOTSP2,10.77.41.154,tcp;(dmc)}} {lines 2} {calls 2} {streams 4} {{primary cm} 10.77.41.11} {{backup cm} } {status {}} {id dmc2} {userid } {{last error} {}} {{delay offer} false} {{Voice Mail client status} {inactive}} {{primary cti} {}} {{backup cti} {}} {{current cti} {}} {{preferred mode} {voip}} {{call type} {voip}} {{domain} {}} {{ixenabled} {no}} {{esrstvernego} {null}} {{ipv4address} {10.77.41.154}} {{ipv6address} {}} {{deploymentmodel} {on-premise}} {{login type} {uc-directory}} {centraluds {}} {{sip port} {42708}} {privacy disabled}

这里我需要比较两个ip,一个是BOTSP2,另一个是ipv4address,两者应该是一样的。

如何提取两个IP地址?

【问题讨论】:

    标签: regex tcl


    【解决方案1】:

    您可以利用以下代码:

    set a {VAPIEI Cmd Rcvd: {state inservice} {type dmc} {description {BOTSP2,10.77.41.154,tcp;(dmc)}} {lines 2} {calls 2} {streams 4} {{primary cm} 10.77.41.11} {{backup cm} } {status {}} {id dmc2} {userid } {{last error} {}} {{delay offer} false} {{Voice Mail client status} {inactive}} {{primary cti} {}} {{backup cti} {}} {{current cti} {}} {{preferred mode} {voip}} {{call type} {voip}} {{domain} {}} {{ixenabled} {no}} {{esrstvernego} {null}} {{ipv4address} {10.77.41.154}} {{ipv6address} {}} {{deploymentmodel} {on-premise}} {{login type} {uc-directory}} {centraluds {}} {{sip port} {42708}} {privacy disabled}}
    regexp {BOTSP2,([\d.]+).*\{ipv4address\}\s*\{([\d.]+)} $a m ip1 ip2
     if {$ip1 eq $ip2} {
       puts "Equal!"
     } 
    

    IDEONE demo

    正则表达式BOTSP2,([\d.]+).*\{ipv4address\}\s*\{([\d.]+)将匹配BOTSP2,,然后捕获到第1组一个或多个数字和点(=ip1),然后它会找到{ipv4address}+空格+{,然后捕获一个或更多数字和点进入第 2 组 (ip2)。

    您可以稍后检查字符串是否相等。

    【讨论】:

    • 你这样写的 $a m ip1 ip2 ...这里的m是什么?
    • m 是整个匹配的子字符串。 $a 是输入。 ipN 是子匹配,捕获的文本。
    【解决方案2】:

    你得到的输出可以很好地解释为一个 Tcl 列表,因此你可以使用lsearch:

    % lsearch -regexp -inline $promptInfo "BOTSP2"
    description {BOTSP2,10.77.41.154,tcp;(dmc)}
    % lsearch -regexp -inline $promptInfo "ipv4address"
    {ipv4address} {10.77.41.154}
    

    第一个结果可以使用lindex 清理并拆分,而第二个结果可以使用单个lindex。如果您想在一行中获取 IP:

    % lindex [split [lindex [lsearch -regexp -inline $promptInfo "BOTSP2"] 1] ,] 1
    10.77.41.154
    % lindex [lsearch -regexp -inline $promptInfo "ipv4address"] 1
    10.77.41.154
    

    之后您可以比较它们。

    【讨论】:

      猜你喜欢
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多