提问人:PopSmoke 提问时间:9/3/2023 最后编辑:toolicPopSmoke 更新时间:9/3/2023 访问量:95
苦苦于 Perl 正则表达式:从文本中提取场景块
Struggling with Perl regex: Extracting scenario blocks from text
问:
我在正则表达式上苦苦挣扎,但首先这是我的输入:
#
#------------------------------------------- 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/);
}
请帮忙
答:
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;
评论
perl -ne 'print if !/^\s*#(?!@)/'
s/^\s*#(?!@).*\n//mg
while(<$fh>) { push @scenario, $_ if (/pattern/); }
my @scenario = grep { /pattern/ } <$fh>;