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); }更多推荐
发布评论