正则表达式在 Expect 缓冲区中查找字符串

发布于 2024-08-16 20:34:42 字数 833 浏览 5 评论 0原文

我正在尝试找到一个可匹配转义字符字符串的正则表达式(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若无相欠,怎会相见 2024-08-23 20:34:42

如果您只想要末尾的数字,那么这应该足够了...

[0-9]{6}

使用新信息更新

假设 \n 是换行符,而不是文字 \ 后跟文字 n,您可以这样做...

\r\n\u001B\[1;14H(X[0-9]{5})

If you just want the number at the end, then this should be enough...

[0-9]{6}

Update with new information

Assuming \n is a newline character, rather than a literal \ followed by a literal n, you can do this...

\r\n\u001B\[1;14H(X[0-9]{5})
我很坚强 2024-08-23 20:34:42

经过更多挖掘,我发现了一些事情。首先,我没有查看程序的输出,而是查看用户的输入。我需要添加“-o”标志来查看程序输出。我还将正则表达式缩短为必要的部分。

来自 @rikh 的正则表达式示例让我了解了为什么他或我自己的正则表达式失败了,这是因为我没有查看输出而是查看了输入。因此,我尝试的原始正则表达式没有错误,但正在查看的数据(缺少“-o”标志)

这是我的问题的完整答案。

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 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.

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"
    }   
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文