苦苦于 Perl 正则表达式:从文本中提取场景块

Struggling with Perl regex: Extracting scenario blocks from text

提问人:PopSmoke 提问时间:9/3/2023 最后编辑:toolicPopSmoke 更新时间:9/3/2023 访问量:95

问:

我在正则表达式上苦苦挣扎,但首先这是我的输入:

 #
  #------------------------------------------- spaceholder ---------------------------------------------------------------------------
  #

  #@E2E-1 @id:1 
  Scenario: Login & Search: B2B_PKG_IN >> BE
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message



  #@E2E-2 @id:32 
  Scenario: Login & Search: B2B_PKG_IN >> NL
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message


  #
  #------------------------------------------- B2B_PKG_3PA ---------------------------------------------------------------------------


  #

  @E2E-3 @id:3
  Scenario: Login & Search: B2B_PKG_3PA >> BE
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message

这是我试图存档的输出:

 #@E2E-1 @id:1 
  Scenario: Login & Search: B2B_PKG_IN >> BE
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message



  #@E2E-2 @id:32 
  Scenario: Login & Search: B2B_PKG_IN >> NL
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message



  @E2E-3 @id:3
  Scenario: Login & Search: B2B_PKG_3PA >> BE
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message

我在正则表达式测试网站上尝试了这个模式,它运行良好:

((@[^\n]+|#@[^\n]+)?\s*)?Scenario:[^\n]*\n(?:[^\n]*\n)*?\n 

然而,当我在 Perl 中使用它时,它似乎根本不起作用。

这就是我想在我的代码中使用它的方式:

while (my $block = <$initial_fh>) {
        push @scenarios, $block if ($block =~ /$pattern/);
    }

请帮忙

正则表达式 Perl 解析

评论

0赞 Steffen Ullrich 9/3/2023
目前尚不清楚您希望如何应用正则表达式,即通过逐行处理输入或将其应用于单个输入字符串。对于逐行读取文件时,以下内容似乎会产生您想要的内容:.对于将所有内容都放在一个字符串中:;有关详细信息,请参阅 perlre 中的 lookbehind 主题perl -ne 'print if !/^\s*#(?!@)/'s/^\s*#(?!@).*\n//mg
0赞 PopSmoke 9/3/2023
对不起,我应该添加这部分: while (my $block = <$initial_fh>) { push @scenarios, $block if ($block =~ /$pattern/); } 我想将每个块推入场景数组
1赞 Casimir et Hippolyte 9/3/2023
请注意,这只不过是while(<$fh>) { push @scenario, $_ if (/pattern/); }my @scenario = grep { /pattern/ } <$fh>;

答:

2赞 Chris Charley 9/3/2023 #1

您可以将输入分隔符 、 设置为段落模式,然后仅捕获以 开头的段落。您只能在下面使用您的实际文件名。$//^\s*#?@/open(...

#!/usr/bin/perl
use strict;
use warnings;


my $file =<<'EOF';
#
  #------------------------------------------- spaceholder ---------------------------------------------------------------------------
  #

  #@E2E-1 @id:1 
  Scenario: Login & Search: B2B_PKG_IN >> BE
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message



  #@E2E-2 @id:32 
  Scenario: Login & Search: B2B_PKG_IN >> NL
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message


  #
  #------------------------------------------- B2B_PKG_3PA ---------------------------------------------------------------------------


  #

  @E2E-3 @id:3
  Scenario: Login & Search: B2B_PKG_3PA >> BE
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message
EOF

local $/ = ''; # enable 'paragraph' mode (blocks separated by 2 or more \n)
my @scenarios;

open my $fh, '<', \$file;
while (<$fh>) {
    chomp;
    push (@scenarios, $_) if /^\s*#?@/;
}

print join "\n\n", @scenarios;

指纹:

  #@E2E-1 @id:1
  Scenario: Login & Search: B2B_PKG_IN >> BE
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message

  #@E2E-2 @id:32
  Scenario: Login & Search: B2B_PKG_IN >> NL
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message

  @E2E-3 @id:3
  Scenario: Login & Search: B2B_PKG_3PA >> BE
    Given I am on the login page
    When I enter <username> and incorrect password multiple times
    Then I should be locked out of my account
    And I should see a lockout message

(注意:您的代码已更正)

!/usr/bin/perl
use strict;
use warnings;

open my $initial_fh, '<', "initial_state.txt_formatted";
open my $current_fh, '<', "current_state.txt_formatted";

my @scenarios;

{
    local $/ = ''; # enable 'paragraph' mode (restricted to this block)
    while (<$initial_fh>) {
        chomp;
        push (@scenarios, $_) if /^\s*\#?\@/;
    }
}

print join("\n", @scenarios);

close $initial_fh;
close $current_fh;