的代码可能会是这样的:
use warnings;
use strict;
my @subjects; # declaring a lexical variable to store all the subjects
my $pattern = qr/
\[ \[ # matching two `[` signs
\s* # ... and, if any, whitespace after them
([^]]+) # starting from the first non-whitespace symbol, capture all the non-']' symbols
]]
/x;
# main processing loop:
while (<DATA>) { # reading the source file line by line
if (/$pattern/) { # if line is matched by our pattern
push @subjects, $1; # ... push the captured group of symbols into our array
}
}
print $_, "\n" for @subjects; # print our array of subject line by line
__DATA__
* [[ Virtualbox Guest Additions]] (2011/10/17 15:19)
* [[ Abiword Wordprocessor]] (2010/10/27 20:17)
* [[ Sylpheed E-Mail]] (2010/03/30 21:49)
* [[ Kupfer]] (2010/05/16 20:18)
依我之见,你需要什么可以描述如下:在文件中的每一行试图找到符号的这个序列...
[[, an opening delimiter,
then 0 or more whitespace symbols,
then all the symbols that make a subject (which should be saved),
then ]], a closing delimiter
正如你所看到的,这个描述很自然地转化为一个正则表达式。唯一可能不需要的是/x
正则表达式修饰符,它允许我广泛地评论它。 )
感谢您的答案会打个盹,并尝试答案! – marinara