正则表达式在 Expect 缓冲区中查找字符串
我正在尝试找到一个可匹配转义字符字符串的正则表达式(Expect 响应,请参阅 这个问题)和一个六位数字(第一个字符是字母数字)。
这是我需要识别的整个字符串:
\r\n\u001b[1;14HX76196
最终我需要提取字符串:
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)
}
我需要识别转义字符以验证它是否是该屏幕区域中的字段。所以我需要一个包含第一部分的正则表达式,但反斜杠让我感到困惑...
此外,一旦我在 $number 变量中拥有完整的字符串,如何隔离 Tcl 中另一个变量中的数字?
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
Here's what I have already:
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...
Also once I have the full string in the $number variable, how do I isolate just the number in another variable in Tcl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想要末尾的数字,那么这应该足够了...
使用新信息更新
假设 \n 是换行符,而不是文字 \ 后跟文字 n,您可以这样做...
If you just want the number at the end, then this should be enough...
Update with new information
Assuming \n is a newline character, rather than a literal \ followed by a literal n, you can do this...
经过更多挖掘,我发现了一些事情。首先,我没有查看程序的输出,而是查看用户的输入。我需要添加“-o”标志来查看程序输出。我还将正则表达式缩短为必要的部分。
来自 @rikh 的正则表达式示例让我了解了为什么他或我自己的正则表达式失败了,这是因为我没有查看输出而是查看了输入。因此,我尝试的原始正则表达式没有错误,但正在查看的数据(缺少“-o”标志)
这是我的问题的完整答案。
我是 Expect 和 Tcl 的菜鸟,因此,如果其中任何内容没有意义,或者您对交互标志有更多见解,请纠正我。
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.
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.
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.