How

编程入门 行业动态 更新时间:2024-10-27 06:34:31
How-To Perl递归正则表达式(How-To Perl recursive regex)

我有我需要测试的设备的输出,大部分响应是一行,但有时它是两行。 我用一个简单的正则表达式解析一行或两行

if ($prompt =~ /(\s.*?)\r\n(.*)/) { Note('Multiline '.$string); TestPrompt($string, $1); TestPrompt($string, $2); } else { TestPrompt($string, $prompt); }

但是,如果响应超过两行呢? 此代码无法处理额外的线条,我想在我的设计中保持健壮。 有没有办法从正则表达式中捕获用于foreach ?

I have outputs from a device I need to test and mostly the response is one line, but sometimes it is two lines. Which I handle with a simple regex parsing one or two lines

if ($prompt =~ /(\s.*?)\r\n(.*)/) { Note('Multiline '.$string); TestPrompt($string, $1); TestPrompt($string, $2); } else { TestPrompt($string, $prompt); }

But what if the response is more than two lines? This code cannot handle the additional lines and I'd like to be robust in my design. Is there a way to capture from regex for use in a foreach?

最满意答案

为什么不使用split函数来执行此操作? 以下是一些使用示例的链接 。 举个例子,为什么不这样做:

my @lines=split /\r\n/,$prompt; Note("Multiline $string") if @lines>1; foreach my $line (@lines) { TestPrompt($string, $line); }

Why not use the split function instead to do this? Here is a link to some examples of usage. For your example, why not do this:

my @lines=split /\r\n/,$prompt; Note("Multiline $string") if @lines>1; foreach my $line (@lines) { TestPrompt($string, $line); }

更多推荐

本文发布于:2023-07-26 16:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278191.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!