如何從正則表達式匹配的結果創建$標量? 有沒有什麼辦法,一旦腳本匹配正則表達式,它可以被分配給一個變量,所以它可以在以後使用,在塊之外。 IE。如果$regex_result = blah blah
然後做一些事情。 我明白我應該使正則表達式儘可能不貪婪。Perl如何將虛擬分配給正則表達式匹配結果
#!/usr/bin/perl
use strict;
use warnings;
# use diagnostics;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';
my @Qmail;
my $regex = "^\\s\*owner \#";
my $sentence = $regex =~ "/^\\s\*owner \#/";
my $outlook = Win32::OLE->new('Outlook.Application')
or warn "Failed Opening Outlook.";
my $namespace = $outlook->GetNamespace("MAPI");
my $folder = $namespace->Folders("test")->Folders("Inbox");
my $items = $folder->Items;
foreach my $msg ($items->in) {
if ($msg->{Subject} =~ m/^(.*test alert) /) {
my $name = $1;
print " processing Email for $name \n";
push @Qmail, $msg->{Body};
}
}
for(@Qmail) {
next unless /$regex|^\s*description/i;
print; # prints what i want ie lines that start with owner and description
}
print $sentence; # prints ^\\s\*offense \ # not lines that start with owner.
哪條線你有問題,你想從正則表達式得到什麼結果? –
嗨腳本的作品,例如$正則表達式返回字符串中的'owner is blah blah'。但是,當我打印$正則表達式出塊時,它只是打印正則表達式,即^ \\ s \ *進攻\# – user3360439
裏面塊$正則表達式返回「所有者是等等等等」,當你打印$正則表達式塊嗎?根本不清楚。 –