简体   繁体   English

TCL脚本中使用的regexp表达式中的变量不匹配

[英]Variable in regexp expression used in TCL script does not match

The below code snippet fetches the agent container ID from the docket host and matches it with the container ID obtained on logging into agent container. 以下代码段从摘要主机获取代理容器ID,并将其与登录到代理容器时获得的容器ID相匹配。 This was written to validate if the agent container login is successful 这是为了验证代理容器登录是否成功而编写的

   set var_op1 [VIR-APP exec "docker ps -aqf \"name=agent\""]

   set docker_op [VIR-APP exec "docker exec -it agent /bin/bash"]

   set var_op2 [VIR-APP exec "uname -a"]

   set result [regexp -nocase ".*$var_op1.*" $var_op2 match]

The regular expression i put is not matching.Could someone help? 我输入的正则表达式不匹配。有人可以帮忙吗?

In this case, you can check for if a string is inside another string more simply with the string first command: 在这种情况下,您可以使用string first命令更简单地检查一个字符串是否在另一个字符串内:

if {[string first $var_op1 $var_op2] >= 0} {
    # The $var_op1 string was found inside $var_op2
}

However you might find that this fails because the result of retrieving the value in var_op1 contains extra whitespace (eg, a trailing newline character). 但是,您可能会发现此操作失败,因为检索var_op1中的值的结果包含额外的空格(例如,尾随换行符)。 Because of that, you probably want to actually do: 因此,您可能想实际执行以下操作:

if {[string first [string trim $var_op1] $var_op2] >= 0} {
    # The $var_op1 string was found inside $var_op2
}

(I believe that this is what is causing you problems with the regexp search, but any additional metadata characters would also cause problems; it's better to use string first here. Failing that, there's also string match , which uses glob-style matching such as most people are familiar with from filename handling, but that's a less correct tool right here.) (我相信这是导致regexp搜索出现问题的原因,但是任何其他元数据字符也会引起问题;最好string first在这里使用string first 。否则,还会有string match ,它使用glob样式匹配,例如大多数人都熟悉文件名处理,但是这里的工具不太正确。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM