Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 基于条件的perl正则表达式提取_Regex_Perl - Fatal编程技术网

Regex 基于条件的perl正则表达式提取

Regex 基于条件的perl正则表达式提取,regex,perl,Regex,Perl,我正在开发一个框架,我必须得到一些正则表达式,但在这一点上被卡住了 Execution start time 09/13/2013 02:43:55 pm [Case-Url] - www.google.com [Req-URL ] - www.qtp.com ***Passed*** __________________________________________________________ [Case-Url] - www.yahoo.com [Req-URL ] -

我正在开发一个框架,我必须得到一些正则表达式,但在这一点上被卡住了

Execution start time 09/13/2013 02:43:55 pm

[Case-Url] - www.google.com


[Req-URL ] - www.qtp.com


***Passed***
__________________________________________________________

[Case-Url] - www.yahoo.com


[Req-URL ] - www.msn.com


***Passed***

___________________________________________________________

[Case-Url] - www.google.com


[Req-URL ] - www.qtp.com


***Failed***

在上面的测试结果中,我必须为通过和失败的测试用例获取[Case URL]和[Req URL]。如何仅获取传递结果的Case URL和Req URL

正则表达式在这里不太合适。相反,将您的输入拆分为块,然后分别进行解析:

use strict; use warnings; use feature 'say';

<DATA>; # discard first line;
# set record separator
local $/ = "__________________________________________________________\n";
while (my $chunk = <DATA>) {
  my ($case, $req, $statusline) = split /\n/, $chunk;
  # possibly parse $case and $req further here
  if ($statusline =~ /Passed/) {
    say for $case, $req;
  }
}

__DATA__
Execution start time 09/13/2013 02:43:55 pm
[Case-Url] - www.google.com
[Req-URL ] - www.qtp.com
***Passed***
__________________________________________________________
[Case-Url] - www.yahoo.com
[Req-URL ] - www.msn.com
***Passed***
___________________________________________________________
[Case-Url] - www.google.com
[Req-URL ] - www.qtp.com
***Failed***

这将提取失败的案例。然后,您可以轻松地从
$fifo[0]
$fifo[3]
提取案例Url和请求Url。对于通过的案例也可以这样做

#!/usr/bin/perl 

use strict;
use warnings;

my @fifo=('') x 7; # Initialize an empty array with size = 7 (Message Block Size)
open(FILE,"temp.txt");

while(<FILE>)
{
    push(@fifo,$_);       # Add element to the end of array making its size 6  
    shift @fifo;          # Remove first element reverting its size back to 5  
    if($fifo[6]=~/Failed/) # Check if 7th line of block has Failed in it 
    {
        print @fifo;
    }
}
close(FILE);
#/usr/bin/perl
严格使用;
使用警告;
my@fifo=('')x 7;#初始化大小为7(消息块大小)的空数组
打开(文件“temp.txt”);
while()
{
push(@fifo,$#)将元素添加到数组的末尾,使其大小为6
shift@fifo;#移除第一个元素,将其大小恢复为5
如果($fifo[6]=~/Failed/)#检查块的第7行是否失败
{
打印@fifo;
}
}
关闭(文件);

除了正则表达式对该特定应用程序的适用性之外,下面是一个正则表达式,它将捕获传递的URL:

\([Case-Url\] - .*)\n+(\[Req-URL \] - (.*)\n+\*{3}Passed\*{3}

我无法在regexplanet中以Perl模式获得这项工作,但您可以在Rubular上看到它的实际操作

Hi,感谢您的回复。我们几乎接近了,但我有1000个Case Url和Req Url,上面的代码显示了输入文件中包含这些Url的所有行。我尝试了以下代码;虽然(my$temp=){my$case,$req,$status)=split/\n/,$temp;print“$case\n”;}但它显示了所有行,而不是前两个case和req URL。@ankitdhebar我的代码对我有效,我不知道您使用的是什么输入。请注意,我仅在
$status
包含
/Passed
时打印URL。您的代码段不包含此条件!
\([Case-Url\] - .*)\n+(\[Req-URL \] - (.*)\n+\*{3}Passed\*{3}