正则表达式在Expect缓冲区中查找字符串 [英] Regular Expression to find string in Expect buffer
问题描述
我正在尝试找到一个匹配字符串转义符的正则表达式(期望响应,请参见
I'm trying to find a regex that works to match a string of escape characters (an Expect response, see this question) and a six digit number (with alpha-numeric first character).
这是我需要识别的整个字符串:
Here's the whole string I need to identify:
\r\n\u001b[1;14HX76196
最终我需要提取字符串:
Ultimately I need to extract the string:
X76196
这是我已经拥有的:
interact {
#...
#...
#this expression does not identify the screen location
#I need to find "\r\n\u001b[1;14H" AND "([a-zA-Z0-9]{1})[0-9]{5}$"
#This regex was what I was using before.
-nobuffer -re {^([a-zA-Z0-9]{1})?[0-9]{5}$} {
set number $interact_out(0,string)
}
我需要标识转义字符以验证它是该屏幕区域中的一个字段.所以我需要一个包含第一部分的正则表达式,但是反斜杠使我感到困惑...
I need to identify the escape characters to to verify that it is a field in that screen region. So I need a regex that includes that first portion, but the backslashes are confusing me...
在$ number变量中包含完整的字符串后,如何仅将数字隔离在Tcl的另一个变量中?
Also once I have the full string in the $number variable, how do I isolate just the number in another variable in Tcl?
推荐答案
我发现了一些需要进一步挖掘的东西.首先,我不是在查看程序的输出,而是在查看用户的输入.我需要添加"-o"标志来查看程序输出.我也将正则表达式缩短为必要的部分.
I found out a few things with some more digging. First of all I wasn't looking at the output of the program but the input of the user. I needed to add the "-o" flag to look at the program output. I also shortened the regex to just the necessary part.
来自 @rikh 的正则表达式示例使我了解了他或我自己的正则表达式失败的原因,这是由于我不是在看输出而是在看输入.因此,我尝试的原始正则表达式不是错误的,而是正在查看的数据(缺少"-o"标志)
The regex example from @rikh led me to look at why his or my own regex was failing, and that was due to the fact that I wasn't looking at the output but the input. So the original regex that I tried wasn't at fault but the data being looked at (missing the "-o" flag)
这是我问题的完整答案.
Here's the complete answer to my problem.
interact {
#...
-o -nobuffer -re {(\[1;14H[a-zA-Z0-9]{1})[0-9]{5}} {
#get number in place
set numraw $interact_out(0,string)
#get just number out
set num [string range $numraw 6 11]
#switch to lowercase
set num [string tolower $num]
send_user " stored number: $num"
}
}
我是一个带有Expect和Tcl的菜鸟,因此,如果其中任何一个都没有意义,或者您对交互标志有更多的了解,请直接告诉我.
I'm a noob with Expect and Tcl so if any of this doesn't make sense or if you have any more insights into the interact flags, please set me straight.
这篇关于正则表达式在Expect缓冲区中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!